重置<选择>更新后的值选择>
我已将我的代码粘贴在下面。基本上,我想更新选择框的选项,然后将其值重置为更新选项之前的值。不幸的是,这不起作用。知道为什么吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
我没有测试它,但我认为它是一个 forloop 错误:
measurements[x].measurement_unit_id
到
x.measurement_unit_id
,也许这会对您有所帮助
I didn't test it, but I think its an forloop error:
measurements[x].measurement_unit_id
to
x.measurement_unit_id
,may be this will help you
您有两个值为
_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.