我将 onchange 函数设置为表行中的选择输入, 我克隆并附加了单击按钮时的行。克隆不响应设置的 onchange 函数。这是到目前为止的代码
<!-- clone and append function-->
$("button").click(function() {
var clone = $('#item_row').clone();
$('#item_row').after(clone)
});
$('#quantity').change(function() {
$('.price').text($('.price').val() * $(this).val())
})
<html>
<body>
<table>
<thead>
<tr id="item_row">
<td>
<select id="quantity">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</td>
</tr>
<tr>
<td>
<p class="price">20,000</p>
</td>
</tr>
</thead>
</table>
<button>Fetch Row</button>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</html>
<!-- begin snippet: js hide: false console: true babel: false -->
最佳答案
有几个问题。
首先使用.on如
$(document).on('click', '. quantity', function () {
// do stuff here
});
其次,由于没有唯一标识符,js如何知道您要更改哪个数量选择?您必须使用 rel="unique_id"添加一个并保持运行计数以添加到克隆的行。
第三,不要使用 ID="quantity"使其成为一个类,因为您将拥有多个类
关于javascript - 如何在更改时更改克隆元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45081173/