jQuery ComboBox破解自定义输入

时间:2012-07-18 16:47:05

标签: jquery combobox

我正在使用this combobox的代码。

我认为通过评论这一部分我会很光滑,这将允许文本输入超出无与伦比的查询。

/*Commented out to allow user input
if (!valid) {
    remove invalid value, as it didn't match anything
    $(this).val("");
    select.val("");
    input.data("autocomplete").term = "";
    return false;
}*/

我遇到的问题是我在验证JS中的表单时无法获得此值。关于我如何做到这一点的任何想法?

5 个答案:

答案 0 :(得分:2)

自问题发布和回答以来,JQuery UI组合框的代码已被修改。问题中提供的链接仍然有效,但其中的代码与此主题中讨论的代码不同。

这是一个新的组合框代码(截至2015年2月9日)的解决方案,就像彼得的方法一样。

首先删除此块以防止删除自定义值:

this.input
  .val( "" )
  .attr( "title", value + " didn't match any item" )
  .tooltip( "open" );
this.element.val( "" );
this._delay(function() {
  this.input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
this.input.autocomplete( "instance" ).term = "";

替换为此块以将自定义值添加为选择选项并将其设置为当前值:

this.element.append('<option>' + this.input.val() + '</option>'); //Add the new option
this.element.val(this.input.val()); //select the new option

答案 1 :(得分:1)

所以我解决了这个问题。我创建了一个隐藏变量:

<input id="EnterYourOwnValue" type="hidden" value="Enter New... or Select" />

然后我编辑了部分jquery组合以保存输入EnterYourOwnValue的值:

                            if (!valid) {
                                $("#EnterYourOwnValue").val($(this).val());
                                /*Commented out to allow user input
                                //remove invalid value, as it didn't match anything
                                $(this).val("");
                                select.val("");
                                input.data("autocomplete").term = "";
                                return false;
                                */
                            }

或者将所选值保存到其中:

                        select: function (event, ui) {
                            $("#EnterYourOwnValue").val(ui.item.option.value);
                        ui.item.option.selected = true;
                        self._trigger("selected", event, {
                            item: ui.item.option
                        });

现在自动完成组合框被黑客攻击为一个选择或在一个框中输入您自己的值!

答案 2 :(得分:1)

为什么不添加&#34;无效&#34;像这样输入到选择元素的末尾

if (!valid) {
    select.append('<option>' + $(element).val() + '</option>'); //Add the new option
    select.val($(element).val()); //select the new option
} 

如果您在选择

中使用直接文本值,这只是相关的

我在搜索答案时找到了你的代码,但在看完你的答案后意识到我必须做的事情。

答案 3 :(得分:0)

花了很多年的时间试图让它与多个组合框一起使用。

要让这篇文章与多个组合框一起使用,我必须添加:

if($(select).attr('name') == YourComboBoxName) $("#EnterYourOwnValue").val($(this).val())

答案 4 :(得分:0)

这是在使用多个组合框时为我做的事情

当然我也必须做一个隐藏的输入。

var thisinput = this.element.attr("name");
var value = this.input.val();

    if(thisinput == "YourComboBoxName"){
    $("#EnterYourOwnValue").val(value);
    }
相关问题