doudou20080720 2013-03-06 10:17
浏览 48
已采纳

jQuery clone row - 在特定字段中插入值

Need some assistance here.

So basically I've got a form which has below fields. Once "Add More Products" button is clicked it clones the product, stock and qty fields and creates a new row. The Product field is a dropdown list of products (query from mysql)

What I need help with is if someone chooses a product I need it to query the database and show stock levels on the chosen product. I've got it working with one row but can't get it to work on the cloned elements (respectively). Code is below. Would really appreciate some assistance.

<select name="product[]" id="product">
    <option value="product 1"> Product 1</option>
    <option value="product 2"> Product 2</option>
    <option value="product 3"> Product 3</option>
    <option value="product 4"> Product 4</option>
</select>
<input type="text" id="stockclass="inputfield" name="stock" size="8" />


$('#product').on("change", function() {
    var val = $(this).val();
    $("#stock").val(val2);                        
});

$("#add").on("click", function() {
    var row = $('#item tbody > tr:last').clone(true);            
    row.insertAfter('#itemsTable tbody > tr:last');
   return false;            
});
  • 写回答

1条回答 默认 最新

  • duansha3771 2013-03-06 10:19
    关注

    Use .on() because .live() is depreciated from now on.

    $(document).on('click','SELECTOR',function(){ /*...*/ });
    

    You are cloning DOMs with the same ID that is totally wrong. Use Class.

    Example

    <select name="product[]" class="product">
        <option value="product 1"> Product 1</option>
        <option value="product 2"> Product 2</option>
        <option value="product 3"> Product 3</option>
        <option value="product 4"> Product 4</option>
    </select>
    <input type="text" class="quantity inputfield" name="quantity[]" size="8" value="" />
    <input type="text" class="target_price inputfield" name="target_price[]" size="8" value="" />
    <input type="text" class="stock_availability inputfield" name="stock_availability[]" size="8" value="" readonly="readonly" />
    

    Now other one is you have click event for selectbox that is wrong it should be change event.

    AS

    $('.product').on('change',function()
    {
    
        $.ajax(
        {
            //ajax params and options and in success set value
        });
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog