如何使用 jQuery 克隆后获取选定的值?
这是我的完整测试代码,但未能获取该值:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-us" />
<title>Job Search</title>
<script language="javascript" type="text/javascript" src="script/jquery-1.3.2.js"></script>
<script language="javascript">
$(document).ready(function(){
$('#test').click(function(){
$('#container').clone().attr('id', 'container2').find('select').each(function() {
var $elem = $(this);
var value = $elem.val();
alert(value);
});
});
});
</script>
</head>
<body>
<div id="container">
<select>
<option value="">--</option>
<option value="Service">Service</option>
<option value="Sales">Sales</option>
<option value="Marketing">Marketing</option>
<option value="Finance">Finance</option>
<option value="Engineering">Engineering</option>
<option value="Management">Management</option>
</select>
</div>
<input type="button" id="test" />
</body>
</html>
Here is my complete testing code,which failed to get the value:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="en-us" />
<title>Job Search</title>
<script language="javascript" type="text/javascript" src="script/jquery-1.3.2.js"></script>
<script language="javascript">
$(document).ready(function(){
$('#test').click(function(){
$('#container').clone().attr('id', 'container2').find('select').each(function() {
var $elem = $(this);
var value = $elem.val();
alert(value);
});
});
});
</script>
</head>
<body>
<div id="container">
<select>
<option value="">--</option>
<option value="Service">Service</option>
<option value="Sales">Sales</option>
<option value="Marketing">Marketing</option>
<option value="Finance">Finance</option>
<option value="Engineering">Engineering</option>
<option value="Management">Management</option>
</select>
</div>
<input type="button" id="test" />
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将 find 方法中的选择器更改为选择 >选择中的选项。
另外,如果您不打算将元素附加到 DOM,为什么还要克隆该元素呢?
要获取克隆元素的选定值,您可以使用
如果您需要将克隆元素插入到 DOM,您可以使用
完整代码在将克隆元素插入到 DOM 后获取选定选项值
按钮单击的完整代码
Changed the selector in find method to select > option from select.
Also why are you cloning the element if you are not going to append it to the DOM.
To get the selected value of the cloned element you can use
If you need to insert the cloned element to the DOM you can use
and full code for getting the selected option value after inserting the cloned element to the DOM
Complete code for the button click
我已经尝试过了,这有效。
I've tried it, and this works.
要迭代页面上的所有选择元素并提醒所选选项的值:
To iterate over all select elements on the page and alert the selected options' values:
你是对的。
克隆
会丢失所选值。我所做的是,在使用克隆时,我查找具有相同 id 的实际元素并获取其值。
即从实际元素而不是克隆元素获取值
如下所示,
You are right. The
clone
loses the selected value.What I did was, while working with the clone I look-up the actual element with same
id
and get its value.i.e. Get the value from the actual element instead of the clone
Like below,
});
在
这里,我可以从克隆表中获取各个输入类型值,然后使用 split 方法来分割这些值,然后找到克隆表的长度并将这些值分配给相应的 bean 值。
});
}
Here i can get the individual input type values form clone table and then using the split method to split these values and after i find the length of clone table and assign these values to corresponding bean values.