2017-02-12 77 views
1

我对于不同的行选定的选项值有问题。我成功地获得了第一排的价值。我想要做的是不同的行项目有不同的价格。JQuery为不同的行获取选定的选项值

我以前关于“为添加行的JQuery获取选定的期权价值”的问题已经得到解决:Here

现在,我拿出另一个问题是,以获得不同的行不同的价值。图片样品是像下面

My problem sample screenshot

下面是我的JS代码:

var i=$('table tr').length; 
 
$(".tambah_sofa").on('click',function(){ 
 
    /* add new row function start here */ 
 
    html = '<tr id="row_'+i+'">'; 
 
    html += '<td><button type="button" id="delete-button" data-row-delete="row_'+i+'">X</button></td>'; 
 
    html += '<td><select id="sofa_'+i+'"><option value="10">hai</option><option value="20">20</option> <option value="50">50</option></select></td>'; 
 
    html += '<td>X</td>'; 
 
    html += '<td><input type="number" name="quantity[]" id="quantity_'+i+'" value="1" disabled="disabled"></td>'; 
 
    html += '</tr>'; 
 
    sidebar = '<tr id="row_'+i+'">'; 
 
    sidebar += '<td><input style="width:50%;" name="sofa'+i+'" type="text" id="price_sofa'+i+'" value="" disabled="disabled"></td>'; 
 
    sidebar += '</tr>'; 
 
    $('#table_item').append(html); 
 
    $('#table_sidebar').append(sidebar); 
 

 
    /* Get selected option value start here */ 
 
    $('body').on('change', '#sofa_'+i+'', function() { 
 
    $('#price_sofa'+i+'').val($(this).find('option:selected').val()); 
 
    }); 
 
    /* Get selected option value end here */ 
 
    i++; 
 
});

+0

请发表完整的代码或链接的例子! –

+0

下面是链接例如:[链接](http://lucretech.com/thecuci) –

回答

1
var i = $('table tr').length; 
$(".tambah_sofa").on('click', function() { 
    /* add new row function start here */ 
    html = '<tr id="row_' + i + '">'; 
    html += '<td><button type="button" id="delete-button" data-row-delete="row_' + i + '">X</button></td>'; 
    html += '<td><select id="sofa_' + i + '"><option value="10">hai</option><option value="20">20</option> <option value="50">50</option></select></td>'; 
    html += '<td>X</td>'; 
    html += '<td><input type="number" name="quantity[]" id="quantity_' + i + '" value="1" disabled="disabled"></td>'; 
    html += '</tr>'; 
    sidebar = '<tr id="row_' + i + '">'; 
    sidebar += '<td><input style="width:50%;" name="sofa' + i + '" type="text" id="price_sofa' + i + '" value="" disabled="disabled"></td>'; 
    sidebar += '</tr>'; 
    $('#table_item').append(html); 
    $('#table_sidebar').append(sidebar); 

    /* Get selected option value start here */ 
    (function(index){ 
     $('#sofa_' + index).on('change', function() { 
     $('#price_sofa' + index).val($(this).val()); 
     }); 
    })(i); 
    /* Get selected option value end here */ 
    i++; 
}); 
+0

我不知道为什么,但此代码不适合我:( –

+0

复制时出错了。更正了它,请现在检查。 – Diode

+0

哇!这是工作!非常感谢! –