0

所以我做了这个搜索,它做了它应该做的前端明智的事情。但是,在提交时,我希望查询忽略逗号。

现在我正在使用逗号进行逗号分隔搜索。整件事是,当我提交时;逗号包含在内,因此会弄乱我的搜索值。

有没有办法在提交时忽略逗号?

示例:搜索[Example][Test]实际上会返回Example,Test.

我在这里做了一个小提琴

非常感谢任何建议和帮助。

4

2 回答 2

1
var firster = true;
//capture form submit
$('form.nice').submit(function(e){
    if(firster){
         // if its the first submit prevent default
         e.preventDefault();
         // update input value to have no commas
         var val = $('input').val();
         val = val.replace(/,/g, ' ');
         $('input').val(val);
         // let submit go through and submit
         firster = false;
         $(this).submit();
    }
});

演示

于 2013-02-01T23:42:09.840 回答
0

查看您的个人资料,我猜您正在使用 python 作为服务器端语言。您尝试解决的问题最好在服务器端处理:永远不要依赖前端代码来转义或格式化查询中使用的数据...查看 Bobby Tables了解更多信息

无论如何,在python中,你可以试试这个:

ajaxString.replace(",","\", \"")

Thiis 将用 Ih 替换所有逗号" OR ",因此字符串 likesome, keywords被翻译成some", "keywords,只需添加some_field IN ("和关闭")即可形成有效查询。
或者,您可以拆分关键字,并分别处理它们(根据结果的相关程度对结果进行排序时,这可能会派上用场

searchTerms = ajaxString.split(",")
>>>['some','keywords']

我希望这应该对你有所帮助。

Lastly, I'd suggest just not bothering with developing your own search function at all. Just add a google search to your site, they're the experts. There is just no way you, by yourself, can do better. Or even if you could, just imagine how long it'd take you!
Yes, sometimes a company will create their own search-engine, but only if they have a good reason to do so, and have the resources such an endevour requires. Programming is often all about being "cleverly lazy": Don't reinvent the wheel.

于 2013-02-01T23:53:02.960 回答