使用 jQuery 将选择从一个选择字段复制到许多其他选择字段?

发布于 2024-12-19 19:20:45 字数 1165 浏览 0 评论 0原文

我在表单中有一个选择,旁边有一个按钮。以下是数量未知的其他选择字段。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

油焖大侠 2024-12-26 19:20:45

假设我已经正确理解了这个问题,并且假设您的 HTML 始终遵循该布局,我会建议如下所示:

$(".copy_select").click(function() {
    $("select[class^='offer_']").val($(this).prev().val());
});

请注意,使用 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:

$(".copy_select").click(function() {
    $("select[class^='offer_']").val($(this).prev().val());
});

Note that the use of prev means the select element will always have to directly precede the button element. I've used an "attribute starts-with" selector to select select elements with a class beginning with "offer_".

ヤ经典坏疍 2024-12-26 19:20:45

不确定你原来的例子有什么问题,这就是我认为你想做的。也就是说,单击按钮时,从 offer_1 类的所有其他选择中的 select.offer_1.value_source 中选择当前值;而不是使用 offer_2 类进行选择,该类将由另一个按钮处理。

JavaScript

$('.copy_select').click(function() {
    $selects = $($(this).data('selects'));
    $source = $selects.filter('.value_source');
    $copies = $selects.not('.value_source');
    console.log($selects);
    console.log($source);
    console.log($copies);
    console.log($source.val());
    $copies.val($source.val());
})

HTML

<select class="offer_1 value_source" name="xnamex">
    <option value="">Please Select</option>
    <option value="a">Text</option>
    <option value="b">Foo</option>
    <option value="c">Bar</option>
</select>
<button class="copy_select" data-selects='.offer_1' type="button" name="Copy" value="">Copy Down</button>
<select class="offer_1" name="xnamex">
    <option value="">Please Select</option>
    <option value="a">Text</option>
    <option value="b">Foo</option>
    <option value="c">Bar</option>
</select>

(我在

必需的 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 class offer_1; and not to selects with the class offer_2 which will be handled with another button.

JavaScript

$('.copy_select').click(function() {
    $selects = $($(this).data('selects'));
    $source = $selects.filter('.value_source');
    $copies = $selects.not('.value_source');
    console.log($selects);
    console.log($source);
    console.log($copies);
    console.log($source.val());
    $copies.val($source.val());
})

HTML

<select class="offer_1 value_source" name="xnamex">
    <option value="">Please Select</option>
    <option value="a">Text</option>
    <option value="b">Foo</option>
    <option value="c">Bar</option>
</select>
<button class="copy_select" data-selects='.offer_1' type="button" name="Copy" value="">Copy Down</button>
<select class="offer_1" name="xnamex">
    <option value="">Please Select</option>
    <option value="a">Text</option>
    <option value="b">Foo</option>
    <option value="c">Bar</option>
</select>

(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.

南汐寒笙箫 2024-12-26 19:20:45
$(document).ready(function() {
  $(".copy_select").click(function() {
    // store value
    var v =  $( ".value_source option:selected" ).val();
    // store text
    var t =  $( ".value_source option:selected" ).text();
    $("select[class^='offer_']").append(
         "<option value=\"" + v + "\">" + t + "</option>"
    );
  });
});
$(document).ready(function() {
  $(".copy_select").click(function() {
    // store value
    var v =  $( ".value_source option:selected" ).val();
    // store text
    var t =  $( ".value_source option:selected" ).text();
    $("select[class^='offer_']").append(
         "<option value=\"" + v + "\">" + t + "</option>"
    );
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文