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 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制