我有一个搜索框,我已经使用jQuery来提交表单,如果搜索框文本字段为空,我不知道如何防止提交。任何帮助?防止搜索字段在空的时候使用jQuery提交
这里是我的代码:
<a href="javascript:$('#searchPost').submit()">Search</a>
我有一个搜索框,我已经使用jQuery来提交表单,如果搜索框文本字段为空,我不知道如何防止提交。任何帮助?防止搜索字段在空的时候使用jQuery提交
这里是我的代码:
<a href="javascript:$('#searchPost').submit()">Search</a>
$('#searchPost').submit(function(event) {
if ($(this).find('input').val() == '') {
event.preventDefault();
}
});
document.getElementById('searchPost').addEventListener('submit', function(e) {
if (!document.getElementById('searchBoxID').value)
e.preventDefault();
}, false);
也可以使用jQuery :) – alex 2011-03-03 02:14:06
在头内的标签:
function submitSearch()
{
if (document.FormName.SearchTextBoxName.value) $('#searchPost').submit();
}
更新的链接:
<a href="javascript:submitSearch()">Search</a>
如果你愿意,你可以删除'!=“”';在JavaScript中,空字符串评估为“false”,非空字符串评估为“true”。 – 2011-03-03 02:12:50
真的......好点。我会去做。 – TimFoolery 2011-03-03 02:14:51
为什么绑定jquery内联? – generalhenry 2011-03-03 02:07:37
同意;逻辑代码不应与内容和演示混合。 – 2011-03-03 02:09:08