重置<选择>更新后的值

发布于 2024-11-08 22:08:36 字数 1413 浏览 0 评论 0原文

我已将我的代码粘贴在下面。基本上,我想更新选择框的选项,然后将其值重置为更新选项之前的值。不幸的是,这不起作用。知道为什么吗?

rebuildMeasurements = function(measurements, measurement_unit_id) {

    var selects = $('select.measure, select.yield_measurement_unit');
    var selected_value;


    $(selects).each(function() {

        $(this).data('selected_value', $(this).val());

        console.log('Original Value: ' + $(this).data('selected_value'));
        // this outputs what we would expect, the original value of the select box

        $(this).empty();

        $(this).append('<option value="_new">New Measurement...</option>');

        for(x in measurements) {
            $(this).append('<option value="' + measurements[x].measurement_unit_id + '">' + measurements[x].measurement + '</option>');
        }

        $(this).append('<option value="_new">New Measurement...</option>');

        $(this).val($(this).data('selected_value'));

        console.log($(this).val());
        // this does NOT output what we would expect, but instead outputs the value of
        // the first option in the select box, even though we explicitly set the
        // value to the original value directly above this line.

    });

    $(current_measurement_select).val(measurement_unit_id);

};

这是非常令人尴尬的,但事实证明

,我在添加新选项标签时使用了与原始 HTML 中不同的值参数,这就是值不匹配的原因。当我解决这个问题时,许多代码变体都可以工作。非常感谢大家的帮助:)。

I've pasted my code below. Basically, I want to update the options of a select box and reset it's value afterwards to the value it had prior to the options being updated. Unfortunately this is not working. Any idea why?

rebuildMeasurements = function(measurements, measurement_unit_id) {

    var selects = $('select.measure, select.yield_measurement_unit');
    var selected_value;


    $(selects).each(function() {

        $(this).data('selected_value', $(this).val());

        console.log('Original Value: ' + $(this).data('selected_value'));
        // this outputs what we would expect, the original value of the select box

        $(this).empty();

        $(this).append('<option value="_new">New Measurement...</option>');

        for(x in measurements) {
            $(this).append('<option value="' + measurements[x].measurement_unit_id + '">' + measurements[x].measurement + '</option>');
        }

        $(this).append('<option value="_new">New Measurement...</option>');

        $(this).val($(this).data('selected_value'));

        console.log($(this).val());
        // this does NOT output what we would expect, but instead outputs the value of
        // the first option in the select box, even though we explicitly set the
        // value to the original value directly above this line.

    });

    $(current_measurement_select).val(measurement_unit_id);

};

Answered

This is terribly embarrassing, but it turns out I was using a different value parameter when adding the new option tags than I was in the original HTML, which is why the values did not match up. Many of these code variants work when I fixed that problem. Thanks so much for the help everybody :).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

少跟Wǒ拽 2024-11-15 22:08:36

试试这个:

$(this).find('option[value=' + $(this).data('selected_value') + ']').attr('selected', 'selected');

Try this:

$(this).find('option[value=' + $(this).data('selected_value') + ']').attr('selected', 'selected');
追风人 2024-11-15 22:08:36

我没有测试它,但我认为它是一个 forloop 错误:

 for(x in measurements) {
                $(this).append('<option value="' + measurements[x].measurement_unit_id + '">' + measurements[x].measurement + '</option>');
            }



  x here is an item in measurements, so you must change it from  :

measurements[x].measurement_unit_id


x.measurement_unit_id

也许这会对您有所帮助

I didn't test it, but I think its an forloop error:

 for(x in measurements) {
                $(this).append('<option value="' + measurements[x].measurement_unit_id + '">' + measurements[x].measurement + '</option>');
            }



  x here is an item in measurements, so you must change it from  :

measurements[x].measurement_unit_id

to
x.measurement_unit_id,

may be this will help you

山人契 2024-11-15 22:08:36

您有两个值为 _new 的选项,这可能会导致该特定选项出现问题,但除此之外,代码没有问题。

我在 jsfiddler 中尝试了一下: http://jsfiddle.net/CXrqp/

我添加了一个前缀参数函数以便我可以看到更改,但除此之外它是相同的代码。

检查您是否发现代码的使用有任何差异。

You have two options with the value _new, which might cause problems for that specific option, but other than that there is no problem with the code.

I tried it out in jsfiddler here: http://jsfiddle.net/CXrqp/

I added a prefix parameter to the function so that I can see the change, but other than that it's the same code.

Check if you see any difference in the usage of the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文