自动完成两个文本字段jquery mobile

时间:2013-08-05 10:38:24

标签: jquery html jquery-mobile

我有以下jquery移动脚本,可为两个文本字段customerstore提供自动完成功能。

$(function() {
    $("#customer").keyup(function(event) {
            if(document.getElementById("customer").value == "")
                $( "#store" ).prop( 'disabled', true );
    });

    $('#customer').autocomplete({
        source: "./SearchCustomer.php?term="+document.getElementById("customer").value,
        minLength: 0,
        focus: function( event, ui ) {
            $( "#customer" ).val( ui.item.label );
            return false;
        },
        select: function (event, ui) {
            $( "#customer" ).val( ui.item.label );
            $( "#customer-id" ).val( ui.item.value );
            $("#store").removeAttr("disabled").focus();
            return false;
        }
    })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      $(ul).attr('data-role', 'listview');
      $(ul).listview();
      return $( "<li>" ).append( "<a>" + item.label + "</a>" ).appendTo( ul );
    };

    $('#store').autocomplete({
        source: "./SearchStore.php?customer="+document.getElementById("customer").value+"&searchText="+document.getElementById("store").value,
        minLength: 0,
        focus: function( event, ui ) {
            $( "#store" ).val( ui.item.label );
            return false;
        },
        select: function (event, ui) {
            $( "#store" ).val( ui.item.label );
            $( "#store-id" ).val( ui.item.value );
            return false;
        }
    })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      return $( "<li>" ).append( "<a>" + item.label + "</a>" ).appendTo( ul );
    };

});

这是html:

<label for="name">Customer:</label>
<input type="text" name="customer" id="customer" value="" />
<input type="hidden" id="customer-id" />
<p id="customer-description"></p>

<label for="name">Store:</label>
<input type="text" name="store" id="store" value=""/>
<input type="hidden" id="store-id" />
<p id="store-description"></p>

客户文本框的自动完成功能正在运行,但不适用于商店文本字段。 我在商店自动完成中复制了相同的customer自动完成代码,我确信数据是从SearchStore.php返回的。

任何解决此问题的建议都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码:

 $(function() {
    $("#customer").keyup(function(event) {
            if(document.getElementById("customer").value == "")
                $( "#store" ).prop( 'disabled', true );
    });

    $('#customer').autocomplete({
        source: "./SearchCustomer.php?term="+document.getElementById("customer").value,
        minLength: 0,
        focus: function( event, ui ) {
            $( "#customer" ).val( ui.item.label );
            return false;
        },
        select: function (event, ui) {
            $( "#customer" ).val( ui.item.label );
            $( "#customer-id" ).val( ui.item.value );
            $("#store").removeAttr("disabled").focus();
            return false;
        }
    })

.each(function() {
    $(this).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      $(ul).attr('data-role', 'listview');
      $(ul).listview();
      return $( "<li>" ).append( "<a>" + item.label + "</a>" ).appendTo( ul );
    };
    }

    $('#store').autocomplete({
        source: "./SearchStore.php?customer="+document.getElementById("customer").value+"&searchText="+document.getElementById("store").value,
        minLength: 0,
        focus: function( event, ui ) {
            $( "#store" ).val( ui.item.label );
            return false;
        },
        select: function (event, ui) {
            $( "#store" ).val( ui.item.label );
            $( "#store-id" ).val( ui.item.value );
            return false;
        }
    })
.each(function() {
    $(this).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      return $( "<li>" ).append( "<a>" + item.label + "</a>" ).appendTo( ul );
    };
    }

});