如果 jquery 自动完成组合框字段为空,则阻止提交
我在我的 Web 应用程序中使用 Jquery 自动完成组合框实用程序(非常好的工具),但如果与自动完成组合框关联的字段为空,我想阻止表单提交。
所以现在,如果通过组合框没有选择任何内容,我只是不显示提交按钮,但是如果选择了某些内容然后删除了......我不知道何时(或何处)必须隐藏我的提交按钮。
有什么想法吗?
提前致谢。
在这里编辑我的代码:
/* HTML */
//The select filled with data for the combobox
<select id="combobox">
<option value=""></option>
<c:forEach var="item" items="${itemList}">
<option value="${item.value}">${item.key}</option>
</c:forEach>
</select>
//The form with the button
<form name="updateForm" action="update.htm" method="post">
<input type="hidden" id="id1" name="cID" />
<input type="hidden" id="id2" name="rID" />
<input type="submit" id='btn_update' value="Update" />
</form>
/* JAVASCRIPT */
//combobox widget init (...)
$("#btn_update").hide();
$( "#combobox" ).combobox({
selected:selectedFunc
});
I'm using the Jquery Autocomplete combobox util in my web application (really nice tool) but I'd like to prevent a form submission if the field associated with the autocomplete combobox is empty.
So for now, if nothing has been selected via the combobox I just don't display the submit button, but if something has been selected and then erased... I don't when (or where) I've got to hide my submit button.
Any ideas?
Thanks in advance.
Edit here is my code:
/* HTML */
//The select filled with data for the combobox
<select id="combobox">
<option value=""></option>
<c:forEach var="item" items="${itemList}">
<option value="${item.value}">${item.key}</option>
</c:forEach>
</select>
//The form with the button
<form name="updateForm" action="update.htm" method="post">
<input type="hidden" id="id1" name="cID" />
<input type="hidden" id="id2" name="rID" />
<input type="submit" id='btn_update' value="Update" />
</form>
/* JAVASCRIPT */
//combobox widget init (...)
$("#btn_update").hide();
$( "#combobox" ).combobox({
selected:selectedFunc
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没理解错的话,你正在寻找这样的东西,
html
script
这是 jsFiddle 链接
编辑:
无法完全解决您的问题,但也许这个例子将向您展示如何
使用更改事件来控制自动完成,因此如果为空,则隐藏提交按钮,如果不为空则显示提交按钮。
这是 jsFiddle 中的一个示例,
但来自 jquery-ui-autocomplete 文档
意思是在按键时不起作用,必须模糊才能触发。如果您可以将按键事件添加到 jquery-ui-autocomplate 中,它将正常工作。
If I did not understand wrong, you are looking something like this,
html
script
and here is jsFiddle link
EDIT:
Couldn't solve your problem exactly, but maybe this example will show to you the way
you can use change event to control autocomplete, so if it's null, hide submit button, if not null show submit button.
here is an example in jsFiddle
but bad news from jquery-ui-autocomplete document
meaning is that will not work onkeypress, it must be blurred to be triggered. if you can add keypress event to jquery-ui-autocomplate that will work exactly.