.clone() メソッドで作成された複数の行に対する jQuery UI のオートコンプリート

StackOverflow https://stackoverflow.com/questions/3842288

  •  27-09-2019
  •  | 
  •  

質問

私は jQuery をほぼ初めて使用します...説明するチュートリアル/投稿をいくつか見つけました1。テーブルの行を複製する方法 (請求書の詳細を挿入する場合に便利): http://forum.jquery.com/topic/validate-will-not-submit-to-server2.PHP および MySQL で jQuery UI オートコンプリートを使用する方法 (データベース テーブルから製品を取得し、すべてを入力する必要がない場合に便利です): http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/

さて、今までテーブルの行をクローンすることができ、最初の行でAutocmpleteの作業を取得できますが、別の行を追加すると問題が発生し、Autocompleteの作業を期待しています。 stackoverflow.com/questions/1492198/jquery-auto-complete-for-dynamicalytextextboxes)が、私のケースに応募することはありませんでした...

そして、コードは次のようになります。

HTML部分

<table border="0" cellspacing="0" cellpadding="4" class="grid" id="details">
 <thead>
  <tr>
   <th scope="col">Codice</th>
   <th scope="col">Nome</th>
   <th scope="col">Quantità</th>
   <th scope="col">Unità di misura</th>
   <th scope="col">Costo U.</th>
   <th scope="col">Totale</th>
  </tr>
 </thead>
 <tbody>
  <tr id="row_0" class="iterable">
   <td><input type="text" name="det_sku_0" id="det_sku_0" class="sku required" /></td>
   <td><input type="text" name="det_name_0" id="det_name_0" class="name required" /></td>
   <td><input name="det_quantity_0" id="det_quantity_0" type="text" class="required" /></td>
   <td>&nbsp;</td>
   <td><input name="det_price_0" id="det_price_0" type="text" class="required" />€</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<ul>
 <li><a href="javascript:void(0);" id="remove" class="icons icon-0">Rimuovi ultima riga</a></li>
 <li><a href="javascript:void(0);" id="add" class="icons icon-new">Aggiungi riga</a></li>
 <li><input type="submit" name="button" id="button" value="Salva tutto" /></li>
</ul>

jQueryの部分

    function addrow(destination) {
     rowcount = parseInt(parent_row.attr('id').replace('row_',''))+1;
     clonecopy = destination.clone(true);
     clonecopy.attr("class","iterable");
     // update numerical suffixes
     clonecopy.attr("id","row_"+rowcount);
     clonecopy.find('.sku, .name').val('');
     clonecopy.find("input[name^='det_sku']").attr({
      "name": "det_sku_"+rowcount,
      "id": "det_sku_"+rowcount
     });
     clonecopy.find("input[name^='det_name']").attr({
      "name": "det_name_"+rowcount,
      "id": "det_name_"+rowcount
     });
     clonecopy.find("select[name^='det_quantity']").attr({
      "name": "det_quantity_"+rowcount,
      "id": "det_quantity_"+rowcount
     });
     clonecopy.find("select[name^='det_price']").attr({
      "name": "det_price"+rowcount,
      "id": "det_price"+rowcount
     });
     clonecopy.insertAfter(destination);
     $('#det_arrayitems').val(rowcount);
    }

$("#add").click(function() {
        parent_row = $('#details tbody>tr:last');
        addrow(parent_row);
    });    

$('input.sku').autocomplete({
     source: "../json/products.php",
     minLength: 2,
     select: function(event, ui) {
      $(this).parent().siblings().children('input.name').val(ui.item.name);
     }
    })

私もこれを試しました(そして他のいくつかのアイデア)が、うまくいきませんでした

$("#add").live("click", function() {
 parent_row = $('#details tbody>tr:last');
 addrow(parent_row);
 $('input.sku').autocomplete({
  source: "../json/products.php",
  minLength: 2,
  select: function(event, ui) {
   $('input.sku').parent().siblings().children('input.name').val(ui.item.name);
  }
 })
});

助けてくれますか?ありがとう...

役に立ちましたか?

解決

解決策を見つけたので、addrow関数内にオートコンプリートを配置し、これに従って(そして感謝して).clone(true)から単語「true」を削除する必要がありました。 JQuery:オートコンプリートフィールドのクローンを作成するにはどうすればよいですか?

function addrow(destination) {
 rowcount = parseInt(parent_row.attr('id').replace('row_',''))+1;
 clonecopy = destination.clone();
 clonecopy.attr("class","iterable");
 // update numerical suffixes
 clonecopy.attr("id","row_"+rowcount);
 clonecopy.find('.sku, .name').val('');
 clonecopy.find("input[name^='det_sku']").attr({
  "name": "det_sku_"+rowcount,
  "id": "det_sku_"+rowcount
 }).autocomplete({       
  source: "../json/products.php",       
  minLength: 2,
  select: function(event, ui) {
   $(this).parent().siblings().children('input.name').val(ui.item.name);       
  }
 });
 clonecopy.find("input[name^='det_name']").attr({
  "name": "det_name_"+rowcount,
  "id": "det_name_"+rowcount
 });

 clonecopy.find("select[name^='det_quantity']").attr({
  "name": "det_quantity_"+rowcount,
  "id": "det_quantity_"+rowcount
 });
 clonecopy.find("select[name^='det_price']").attr({
  "name": "det_price"+rowcount,
  "id": "det_price"+rowcount
 });
 clonecopy.insertAfter(destination);
 $('#det_arrayitems').val(rowcount);

}

他のヒント

私は、私はすべての行を保持しているdiv要素を持っている類似した何かを、そして行のEACT入力フィールドは、私が呼び出す行を追加するには、それの名前とIDのベース部と、[OK]をクリックします上のrelタグを持っています次:

 $("#addChain").click(function() {  
         var index = $("#chainHolder").children().length + 1;  
        $("#chainHolder").children(":first").clone().each(function() {  
       $(this).find(":input").each(function() {  
                $(this).attr("id", $(this).attr("rel")+"["+index+"]");  
                $(this).attr("name", $(this).attr("rel")+"["+index+"]");  
            });  
             $(this).children(":first").val(index);  
        }).appendTo("#chainHolder");  
    });  

作成ボタンをクラス名のaddmore、そのボタンをクリックし、次の方法です。ください形でテーブルの列のようにこの下の最後に追加されたhtmlを表しています。

//adds extra table rows
var i=$('table tr').length;
$(".addmore").on('click',function(){
     html = '<tr>';
     html += '<td><input class="case" type="checkbox"/></td>';
     html += '<td><input type="text" data-type="productCode" name="itemNo[]" id="itemNo_'+i+'" class="form-control autocomplete_txt" autocomplete="off"></td>';
     html += '<td><input type="text" data-type="productName" name="itemName[]" id="itemName_'+i+'" class="form-control autocomplete_txt" autocomplete="off"></td>';
     html += '<td><input type="text" name="price[]" id="price_'+i+'" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>';
     html += '<td><input type="text" name="quantity[]" id="quantity_'+i+'" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>';
     html += '<td><input type="text" name="total[]" id="total_'+i+'" class="form-control totalLinePrice" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>';
     html += '</tr>';
     $('table').append(html);
     i++;
 });

以下のスクリプトの削除部---削除ボタンをクラス名を削除します。

//deletes the selected table rows
$(".delete").on('click', function() {
   $('.case:checkbox:checked').parents("tr").remove();
   $('#check_all').prop("checked", false); 
});

また簡単なチュートリアルのサンプル請求書システムの機能または右から

  1. 複数のテーブル列の追加と削除
  2. 複数の自動補完、多く

請求システムのjQueryを使ったオートコンプリート

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top