Jquery自动填充不在其他字段中填充

时间:2014-01-08 13:42:09

标签: javascript jquery html autocomplete

情境: 我有一个下拉列表,用户可以在其中选择供应商。根据他们选择的供应商,用户可以使用纸质搜索(使用JQuery Autocomplete)搜索项目。选择某个项目后,descriptionpriceper_pack文本框将填充该特定项目的相关信息(此信息将从我的数据库中提取)。

目前的情况如下: enter image description here

问题: 当用户从纸张搜索中选择项目时,上面说明的文本框没有填充相关信息,并且我不知道为什么会发生这种情况。有谁知道为什么会这样?

这是论文搜索的代码:

$(function() {
window.globalVar = 0;

// Skip the filled description boxes
for (var i=0; i<10; i++)
{
    if($('#description_'+window.globalVar).val() != "")
    {
        window.globalVar++;
    }
}

// Write the paper description and price for the selected paper
function log( message ) {
    var values = message.split('|');
    $('#description_'+window.globalVar).val(values[0]);
    $('#priceper_'+window.globalVar).val(values[1]);
$('#per_pack_'+window.globalVar).val(values[2]);
    window.globalVar++;
    console.log(window.globalVar);
}

// Search the Paper db
$( "#supplier_search" ).autocomplete({
  source: function( request, response ) {
    $.ajax({
      url: "http://mpc.vario/mis_pp/common/pp_json",
      dataType: "jsonp",
      data: {
        featureClass: "P",
        style: "full",
        maxRows: 25,
        name_startsWith: request.term,
        supplier: $('#supplier').val()
      },
      success: function( data ) {
        console.log(data);
        response( $.map( data, function( item ) {
          return {
            label: item.name,
            value: item.name + '|' + item.value + '|' + item.pack
          }
        }));
      }
    });
  },
  minLength: 2,
  select: function( event, ui ) {
    log( ui.item.value );
    $(this).val('');
    return false;
  },
  open: function() {
    $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-stop" );
  },
  close: function() {
    $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
  }
});
 });

这是添加/删除行的代码:

 $(document).ready(function () {
    $counter = 1;
    $('#buttonadd').click(function () {
        $counter++;
        $('#invoiceitems > tbody:last').append('<tr><td><input type="button" class="buttondelete" value="Delete"/></td>\
        <td><input type="text" name="item[' + $counter + '][description]" id="description_" class="description" size="85" maxlength="70" value="" /></td>\
        <td><input type="text" name="item[' + $counter + '][priceper]" id="description_" class="priceper" size="10" maxlength="9" value="" /></td>\
        <td><input type="text" name="item[' + $counter + '][per_pack]" id="per_pack_" class="per_pack" size="10" maxlength="9" value="" /></td>\
        <td><input type="text" name="item[' + $counter + '][quantity]" id="quantity_" class="quantity" size="10" maxlength="9" value="" /></td>\
        <td><label class="subtotal"></label></td></tr>');

    });
    $('table#invoiceitems').on('keyup', '.quantity , .priceper',function () {
        UpdateTotals(this);
    });

    $counter = 1;
       $('table#invoiceitems').on('click','.buttondelete',function () {
        $counter++;
        if($('table#invoiceitems tbody tr').length==1){
            alert('Cant delete single row');
            return false;
        }
        $(this).closest('tr').remove();

    });
    CalculateSubTotals();
    CalculateTotal();
    });

2 个答案:

答案 0 :(得分:0)

视细节字段的数量取决于“行号”(通过window.globalVar)

每当您删除并添加新订单项时,请务必重新计算“有效”行。

设置字段值的代码是此部分:

 $('#description_'+window.globalVar).val(values[0]);
 $('#priceper_'+window.globalVar).val(values[1]);
 $('#per_pack_'+window.globalVar).val(values[2]);

调试您的javascript并在下拉菜单中选中window.globalVar的值,然后从下拉列表中选项。您很可能会发现该值不正确,并且您正在尝试设置文档中不存在的控件的值。

答案 1 :(得分:0)

window.globalVar确实试图将其值设置为不存在的控件。

更新的有效代码:

    $(function() {
    window.globalVar = 1;

    // Write the paper description and price for the selected paper
    function log( message ) {
        var values = message.split('|');
        $('#description_'+window.globalVar).val(values[0]);
        $('#priceper_'+window.globalVar).val(values[1]);
    $('#per_pack_'+window.globalVar).val(values[2]);
        console.log(window.globalVar);
    }

    // Search the Paper db
    $( "#supplier_search" ).autocomplete({
      source: function( request, response ) {
        $.ajax({
          url: "http://mpc.vario/mis_pp/common/pp_json",
          dataType: "jsonp",
          data: {
            featureClass: "P",
            style: "full",
            maxRows: 25,
            name_startsWith: request.term,
            supplier: $('#supplier').val()
          },
          success: function( data ) {
            console.log(data);
            response( $.map( data, function( item ) {
              return {
                label: item.name,
                value: item.name + '|' + item.value + '|' + item.pack
              }
            }));
          }
        });
      },
      minLength: 2,
      select: function( event, ui ) {
        log( ui.item.value );
        $(this).val('');
        return false;
      },
      open: function() {
        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-stop" );
      },
      close: function() {
        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
      }
    });
    });

$(document).ready(function () {
    $counter = 1;
    $('#buttonadd').click(function () {
        $counter++;
        $('#invoiceitems > tbody:last').append('<tr><td><input type="button" class="buttondelete" value="Delete"/></td>\
        <td><input type="text" name="item[' + $counter + '][description]" id="description_' + $counter + '" class="description" size="85" maxlength="70" value="" /></td>\
        <td><input type="text" name="item[' + $counter + '][priceper]" id="priceper_' + $counter + '" class="priceper" size="10" maxlength="9" value="" /></td>\
        <td><input type="text" name="item[' + $counter + '][per_pack]" id="per_pack_' + $counter + '" class="per_pack" size="10" maxlength="9" value="" /></td>\
        <td><input type="text" name="item[' + $counter + '][quantity]" id="quantity_' + $counter + '" class="quantity" size="10" maxlength="9" value="" /></td>\
        <td><label class="subtotal"></label></td></tr>');
    window.globalVar = $counter;

    });
    $('table#invoiceitems').on('keyup', '.quantity , .priceper',function () {
        UpdateTotals(this);
    });

    $counter = 1;
       $('table#invoiceitems').on('click','.buttondelete',function () {
        $counter++;
        if($('table#invoiceitems tbody tr').length==1){
            alert('Cant delete single row');
            return false;
        }
        $(this).closest('tr').remove();
    UpdateTotals(this);

    });
    CalculateSubTotals();
    CalculateTotal();
    });
相关问题