如果 jquery 自动完成组合框字段为空,则阻止提交

发布于 2025-01-07 16:24:21 字数 967 浏览 2 评论 0原文

我在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被你宠の有点坏 2025-01-14 16:24:21

如果我没理解错的话,你正在寻找这样的东西,

html

<select id="sel">
    <option>-select-</option>
    <option value="example1">example1</option>
    <option value="example2">example2</option>
</select>
<input type="submit" id="but" style="display:none;"/>

script

$("#sel").change(function() {

if(this.selectedIndex == 0) 
    $("#but").hide();
else 
    $("#but").show();
});​

这是 jsFiddle 链接


编辑:

无法完全解决您的问题,但也许这个例子将向您展示如何

使用更改事件来控制自动完成,因此如果为空,则隐藏提交按钮,如果不为空则显示提交按钮。

这是 jsFiddle 中的一个示例

但来自 jquery-ui-autocomplete 文档

更改事件

当字段模糊时触发,如果值发生变化;
ui.item 指所选项目。

意思是在按键时不起作用,必须模糊才能触发。如果您可以将按键事件添加到 jquery-ui-autocomplate 中,它将正常工作。

If I did not understand wrong, you are looking something like this,

html

<select id="sel">
    <option>-select-</option>
    <option value="example1">example1</option>
    <option value="example2">example2</option>
</select>
<input type="submit" id="but" style="display:none;"/>

script

$("#sel").change(function() {

if(this.selectedIndex == 0) 
    $("#but").hide();
else 
    $("#but").show();
});​


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

change event

Triggered when the field is blurred, if the value has changed;
ui.item refers to the selected item.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文