英文:
dropdown select not being part upon the process of cloning
问题
克隆代码是有效的,只是下拉选择框没有被包括在克隆过程中。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$("input.tr_clone_add").live('click', function() {
var $tr = $(this).closest('.tr_clone');
var $clone = $tr.clone();
$clone.find('.form-control js-example-basic-single').val('');
// 下面添加获取下拉选择框值的代码
var selectValue = $tr.find('select[name="animal"]').val();
$clone.find('select[name="animal"]').val(selectValue);
$tr.after($clone);
});
<!-- language: lang-html -->
<table>
<tr class="tr_clone">
<td>
<input type="button" name="add" value="clone row" class="tr_clone_add" /><br><br>
<select name="animal" id="number">
<option value="cow">cow</option>
<option value="horse">horse</option>
<option value="dog">dog</option>
<option value="cat">cat</option>
</select>
<select name="vehicle" id="letter">
<option value="car">car</option>
<option value="ship">ship</option>
<option value="plane">plane</option>
</select>
<input name="remarks" type="text">
<br>
<hr>
</td>
</tr>
</table>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<!-- end snippet -->
我尝试的目标是,在克隆行的过程中,也将行的下拉选择框的值传递给要克隆的行。
英文:
Cloning code works it's just that the dropdown select is not being part of the cloning process.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$("input.tr_clone_add").live('click', function() {
var $tr = $(this).closest('.tr_clone');
var $clone = $tr.clone();
$clone.find('.form-control js-example-basic-single').val('');
$tr.after($clone);
});
<!-- language: lang-html -->
<table>
<tr class="tr_clone">
<td>
<input type="button" name="add" value="clone row" class="tr_clone_add" /><br><br>
<select name="animal" id="number">
<option value="cow">cow</option>
<option value="horse">horse</option>
<option value="dog">dog</option>
<option value="cat">cat</option>
</select>
&nbsp;
<select name="vehicle" id="letter">
<option value="car">car</option>
<option value="ship">ship</option>
<option value="plane">plane</option>
</select>
&nbsp;
<input name="remarks" type="text">
<br>
<hr>
</td>
</tr>
</table>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<!-- end snippet -->
What I am trying to do is, also pass the value of dropdown select of row to be cloned upon the process of cloning the row.
答案1
得分: 0
-
**
live
**已被弃用,因此请更新您的jQuery库。 -
对于您的问题:
-
首先查找并获取选定的选项,然后将属性
selected
添加到选定的选项上。 -
要获取动态创建元素的事件,请使用事件委托。
-
示例:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).on("change", "select", function() {
var val = $(this).val();
$("option", this).removeAttr("selected").filter(function() {
return $(this).attr("value") == val;
}).first().attr("selected", "selected");
});
$('body').on('click', 'input.tr_clone_add', function() {
var $tr = $(this).closest('.tr_clone');
var $clone = $tr.clone();
$clone.find('.form-control js-example-basic-single').val('');
$tr.after($clone);
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr class="tr_clone">
<td>
<input type="button" name="add" value="clone row" class="tr_clone_add" /><br><br>
<select name="animal" id="number">
<option value="cow">cow</option>
<option value="horse">horse</option>
<option value="dog">dog</option>
<option value="cat">cat</option>
</select>
<select name="vehicle" id="letter">
<option value="car">car</option>
<option value="ship">ship</option>
<option value="plane">plane</option>
</select>
<input name="remarks" type="text">
<br>
<hr>
</td>
</tr>
</table>
</body>
<!-- end snippet -->
英文:
-
live
is deprecated so update your jQuery library. -
For your question:
-
First of all find and get selected option and add attribute
selected
to selected option. -
To get events of dynamic created elements , use event delegation.
-
Example:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).on("change", "select", function() {
var val = $(this).val();
$("option", this).removeAttr("selected").filter(function() {
return $(this).attr("value") == val;
}).first().attr("selected", "selected");
});
$('body').on('click', 'input.tr_clone_add', function() {
var $tr = $(this).closest('.tr_clone');
var $clone = $tr.clone();
$clone.find('.form-control js-example-basic-single').val('');
$tr.after($clone);
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr class="tr_clone">
<td>
<input type="button" name="add" value="clone row" class="tr_clone_add" /><br><br>
<select name="animal" id="number">
<option value="cow">cow</option>
<option value="horse">horse</option>
<option value="dog">dog</option>
<option value="cat">cat</option>
</select>
&nbsp;
<select name="vehicle" id="letter">
<option value="car">car</option>
<option value="ship">ship</option>
<option value="plane">plane</option>
</select>
&nbsp;
<input name="remarks" type="text">
<br>
<hr>
</td>
</tr>
</table>
</body>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论