Javascript 如何在使用jquery创建组合框时动态设置属性

Javascript 如何在使用jquery创建组合框时动态设置属性,javascript,jquery,combobox,Javascript,Jquery,Combobox,我正在尝试使用jquery转换可搜索组合框中的下拉列表。下面的代码对我来说很好,但需要动态设置onkeypress事件、onchange事件、id、name、maxlength属性值。 我正在使用以下jquery代码创建一个组合框: (function( $ ) { $.widget( "ui.combobox", { _create: function() { var input, that = this, wasOpen =

我正在尝试使用jquery转换可搜索组合框中的下拉列表。下面的代码对我来说很好,但需要动态设置onkeypress事件、onchange事件、id、name、maxlength属性值。 我正在使用以下jquery代码创建一个组合框:

 (function( $ ) {
    $.widget( "ui.combobox", {
    _create: function() {
        var input,
        that = this, 
        wasOpen = false, 
        select = this.element.hide(),
        selected = select.children( ":selected" ),
        value = selected.val() ? selected.val() : "",
        wrapper = this.wrapper = $( "<span>" ).insertAfter( select );

        input = $( "<input>" )
            .appendTo( wrapper ).val(value).attr( "title", "" )
            .attr("onchange","return pincodeValidation(this.value);")
            .attr("onkeypress","return fnNotAlphabet(event);")
            .attr("maxlength","6")
            .attr("id","pinCode")
            .attr("name","pinCode")
            .autocomplete({// code for autocomplete});
        });
      })(jQuery );
(函数($){
$.widget(“ui.combobox”{
_创建:函数(){
var输入,
那=这个,
wasOpen=false,
select=this.element.hide(),
selected=select.children(“:selected”),
value=selected.val()?selected.val():“”,
wrapper=this.wrapper=$(“”)。insertAfter(选择);
输入=$(“”)
.appendTo(wrapper).val(value).attr(“title”,“”)
.attr(“onchange”,“返回pincodeValidation(this.value);”)
.attr(“onkeypress”,“return fnNotAlphabet(事件);”)
.attr(“最大长度”、“6”)
.attr(“id”、“pinCode”)
.attr(“名称”、“pinCode”)
.autocomplete({//code for autocomplete});
});
})(jQuery);
如何动态设置这些元素属性,而不是直接进行更改
在JavaScript文件中?

不使用attr()而应该使用Hope this help

我也在搜索相同的内容,但没有找到解决方案。因此我更改了代码,在选择组合框中的项目时触发onkeypress

$( "#countryInput" ).combobox({ 
    select: function (event, ui) { 
       $( "#countryInput" ).trigger('keypress');
}
});

为什么要使用内联属性来设置事件?使用jquery.on()代替标题我在我的下拉id上调用combobox,比如$(“#pincode”).combobox();这里pincode是一个下拉列表,如果我使用.on(),它将更改下拉列表的事件,而不是使用上面的jquerybind()或现在首选的版本on()动态创建的输入元素