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

时间:2013-06-21 11:04:45

标签: javascript jquery combobox

我正在尝试使用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 );

如何动态设置这些元素属性,而不是直接进行更改  在JavaScript文件中?

2 个答案:

答案 0 :(得分:0)

您应该使用.bind()希望这有助于

,而不是使用attr()

答案 1 :(得分:0)

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

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