更改<select option>在Javascript中,但之前的选择保留,为什么?
Changing the content of a <select option> in Javascript but previous selection stays, why?
我正在尝试做一个简单的脚本,检查日期,如果"选择的日子" == "明天"然后更改包含交付选项的下拉菜单:
Drop down:
0-When?(selected)
1-AM
2-PM
如果day = tomorrow,那么我删除javascript中的选项:
Drop down:
0-When?
2-PM(selected)
脚本:
// remove all options first
document.getElementById('inputheurelivraison').options.length = 0;
if (parseInt(datebits[2]) == parseInt(demain)){//remove am
document.getElementById('inputheurelivraison').options[0] = new Option("WHEN?", 0, false, false);
document.getElementById('inputheurelivraison').options[1] = new Option("PM", 2, true, true); // new Option(text, value, defaultSelected, selected)
alert ("<? echo t(73); ?>");
}
else {//put am
document.getElementById('inputheurelivraison').options[0] = new Option("WHEN?", 0, true, true);
document.getElementById('inputheurelivraison').options[1] = new Option("AM", 1, false, false);
document.getElementById('inputheurelivraison').options[2] = new Option("PM", 2, false, false);
}
问题:
假设有人填写表单,然后选择"AM"作为选项,然后将日期更改为"明天",然后我的脚本运行并从列表中删除"AM"选项,并选择"PM"作为"selected"。当用户提交表单时,POST数据是选中的"AM"…
为什么?我选择了"PM",当我看一看HTML时,它说"PM"为"selected",那么为什么它不提交该值呢?
Thanks a bunch in advance
乔没有必要先删除所有选项,您可以只删除不需要的选项。像下面这样的东西应该是合适的(注意存储对DOM元素的引用,所以只能得到一次):
var doIt = (function() {
// Reference to removed option
var removedOption;
return function(s) {
// Get reference to select once
var select = document.getElementById('inputheurelivraison');
if (parseInt(datebits[2]) == parseInt(demain)) {
// Store reference to am option then remove it
removedOption = select.options[1];
select.removeChild(select.options[1]);
// Make pm selected (it's now index 1)
select.options[1].selected = true;
// Debug?
alert ("<? echo t(73); ?>");
} else {
// Replace removed option
select.insertBefore(removedOption, select.options[1]);
// Make first option selected
select.options[0].selected = true;
}
}
}());
也可以将不需要的选项移动到隐藏的select元素中。
以上只是一个概念证明的例子,有很多方法可以给猫剥皮,底线是你不必每次都删除所有的选项并重新创建你想要的
谢谢你的回答。但我刚刚发现了问题所在。我来解释一下,以防对其他人有帮助。
检查"day"是否="tomorrow"并更改表单后,脚本将用AJAX将数据POST到PHP文件中。所以这里是问题的起源:我要求我的脚本POST我的下拉框的selectedIndex
而不是它的option[selectedIndex].value
。出于这个原因,我的脚本返回"AM"作为选择,即使"PM"被选中,因为selectedIndex
总是从0,1,2等开始,我的value
是AM=1和PM=2。
所以我改了:document.getElementById("inputheurelivraison").selectedIndex
:document.getElementById('inputheurelivraison').options[document.getElementById("inputheurelivraison").selectedIndex].value
在AJAX中发送POST值时,现在它很好。
希望没有人犯同样的错误。
- 我可以通过使用函数绑定来保留默认值吗?如果没有,为什么
- 为什么这个 JavaScript switch 语句(在 for 循环内)保留了上一次迭代的值
- 为什么 Javascript 中的嵌套对象不保留它们的值
- 为什么“;闭合的“;保留字是JS
- 为什么参数不在函数之外保留其值
- 为什么这个 JavaScript 闭包数组保留旧值
- 为什么新创建的输入标签不会保留在页面上
- 为什么 javascript 会保留在外部函数中发生的局部变量重新分配,而不必捕获返回值
- 为什么在尝试在 javascript 中对 URI 组件的保留字符进行编码时出现空白 %3 字符
- 为什么如果失败,我无法保留调用 api
- 为什么我的 JavaScript 哈希带有保留字“toString”作为显示 NaN 的键
- 为什么从其他网络返回会保留最后一个哈希值,同时显示第一个哈希值的内容
- 为什么要在javascript中保留未声明的变量
- 为什么在这种情况下调用 ES6 时“产生”是一个保留字
- 为什么骨干.视图对象仍然保留在内存中
- 为什么是"float"JavaScript中的保留字
- 为什么Gulp不保留dest函数返回的文件名
- 更改
- 为什么他们使用保留关键字'continue'在IndexedDB's Cursor对象中命名一个
- 为什么错误信息&;SyntaxError: let是一个保留标识符require.js:52:2&;在开发firefox