使用 jQuery 将选择从一个选择字段复制到许多其他选择字段?
我在表单中有一个选择,旁边有一个按钮。以下是数量未知的其他选择字段。
<select class="offer_1 value_source" name="xnamex">
<option value="">Please Select</option>
<option value="xxxxxxx">Text</option>
</select>
<button class="copy_select offer_1" type="button" name="Copy" value="">Copy Down</button>
<select class="offer_1" name="xnamex">
<option value="">Please Select</option>
<option value="xxxxxxx">Text</option>
</select>
<!-- unknown quantity of selects follow -->
我通过类 Offer_{number} 将选择字段和按钮分组在一起,
我想将所选值从类 value_source 的一个选择复制到具有相同类 (offer_{number}) 的所有其他选择。
这是我当前的代码:
$(document).ready(function() {
$('.copy_select').click(function() {
attr_class = $(this).attr('class');
attr_class = attr_class.replace('copy_select ', '.');
$(attr_class).val($(attr_class + ' value_source').val());
alert(attr_class);
});
});
所有选择均保持未选中状态,并且我的 copy_source 重置为未选中状态。
我做错了什么?
另外,我如何检查 select 是否设置了值,如果没有则抛出一个 alert()
?
非常非常感谢您的宝贵时间:)
I have a select in a form with a button next to it. Below is an unknown quantity of other select fields.
<select class="offer_1 value_source" name="xnamex">
<option value="">Please Select</option>
<option value="xxxxxxx">Text</option>
</select>
<button class="copy_select offer_1" type="button" name="Copy" value="">Copy Down</button>
<select class="offer_1" name="xnamex">
<option value="">Please Select</option>
<option value="xxxxxxx">Text</option>
</select>
<!-- unknown quantity of selects follow -->
I am grouping the select fields and button together via the class offer_{number}
I want to copy the selected value from the one select with the class value_source to all other selects with the same class (offer_{number}).
Here is my current code:
$(document).ready(function() {
$('.copy_select').click(function() {
attr_class = $(this).attr('class');
attr_class = attr_class.replace('copy_select ', '.');
$(attr_class).val($(attr_class + ' value_source').val());
alert(attr_class);
});
});
All selects remain unselected and my copy_source get reset to unselected.
What am I doing wrong?
Also, how would I check that the select has a value set and throw up an alert()
if not?
Many, many thanks for your time :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设我已经正确理解了这个问题,并且假设您的 HTML 始终遵循该布局,我会建议如下所示:
请注意,使用
prev
表示select
元素必须始终直接位于button
元素之前。我使用了“属性开头”选择器来选择select
具有以“offer_”开头的类的元素。Assuming I've understood the question correctly, and assuming your HTML always follows that layout, I would suggest something like this:
Note that the use of
prev
means theselect
element will always have to directly precede thebutton
element. I've used an "attribute starts-with" selector to selectselect
elements with a class beginning with "offer_".不确定你原来的例子有什么问题,这就是我认为你想做的。也就是说,单击按钮时,从
offer_1
类的所有其他选择中的select.offer_1.value_source
中选择当前值;而不是使用offer_2
类进行选择,该类将由另一个按钮处理。JavaScript
HTML
(我在
必需的 jsFiddle 示例。
Not sure what's wrong with your original example, this does what I think you want to do. That is, when the button is clicked, select the current value from
select.offer_1.value_source
in all other selects with the classoffer_1
; and not to selects with the classoffer_2
which will be handled with another button.JavaScript
HTML
(I'm using a data attribute on the
<button>
to associate the button with the<select>
s because data attributes are awesome and parsing CSS class strings isn't.)Obligatory jsFiddle example.