我怎样才能重写这个javascript代码,以便它引用每个选项框
How can I rewrite this javascript code, so that it will refer to every optionbox?
如何重写这个javascript代码,以便它引用每个选项框(而不仅仅是第一个)?
<html>
<div id="form123">
<select id="day" name="day">
<option value="abc" name="abc" id="abc">abc</option>
<option value="other" name="other" id="other">other...</option>
</select>
<select id="day" name="day">
<option value="abc" name="abc" id="abc">abc</option>
<option value="other" name="other" id="other">other...</option>
</select>
<select id="day" name="day">
<option value="abc" name="abc" id="abc">abc</option>
<option value="other" name="other" id="other">other...</option>
</select>
<script type="text/javascript">
var selectmenu=document.getElementById("day")
selectmenu.onchange=function(){
var chosenoption=this.options[this.selectedIndex]
if (chosenoption.value=="other"){
var entered_date = window.prompt("Enter the date","");
document.getElementById('other').value = entered_date;
document.getElementById('other').text = entered_date;
}
}
</script>
</div>
</html>
到目前为止,对话框仅在我选择第一个选项框时才显示。
即使我选择另一个选项框,我也想显示对话框。
将来,将动态添加选项框。
ID
必须是唯一的。我建议在更改事件的每个select
上使用一个函数,并将this
作为参数传递,例如:
function changeDate(obj) {
var chosenoption = obj.options[obj.selectedIndex];
if (chosenoption.value == "other") {
var winProVal = window.prompt("Enter the date", "");
if (winProVal != null) {
obj.options[obj.value].value = obj.options[obj.value].text = winProVal;
}
}
}
<select class="day" name="day" onchange='changeDate(this);'>
<option value="abc" name="abc" id="abc">abc</option>
<option value="other" name="other" id="other">other...</option>
</select>
<select class="day" name="day" onchange='changeDate(this);'>
<option value="abc" name="abc" id="abc">abc</option>
<option value="other" name="other" id="other">other...</option>
</select>
<select class="day" name="day" onchange='changeDate(this);'>
<option value="abc" name="abc" id="abc">abc</option>
<option value="other" name="other" id="other">other...</option>
</select>
this
是指元素。也用类改变ID
。
ID
应该是唯一的(名称也是如此)。您可以定义元素类并相应地更改您的 js。
<html>
<div id="form123">
<select id="day" name="day" class="day">
<option value="abc" name="abc1" id="abc1">abc</option>
<option value="other" name="other1" id="other1">other...</option>
</select>
<select id="day" name="day" class="day">
<option value="abc" name="abc2" id="abc2">abc</option>
<option value="other" name="other2" id="other2">other...</option>
</select>
<select id="day" name="day" class="day">
<option value="abc" name="abc3" id="abc3">abc</option>
<option value="other" name="other3" id="other3">other...</option>
</select>
<script type="text/javascript">
var selectmenu=document.getElementsByClassName("day");
for (var i = 0; i < selectmenu.length; ++i) {
selectmenu[i].onchange=function(){
var chosenoption=this.options[this.selectedIndex]
if (chosenoption.value=="other"){
var entered_date = window.prompt("Enter the date","");
this.options[1].value = entered_date;
this.options[1].text = entered_date;
}
}
}
</script>
</div>
</html>
相关文章:
- 引用对象中的通用值
- 使用按钮引用单选按钮选项
- AngularJS select with ng选项不更新父作用域中的引用对象属性
- 使用设置选项时未引用高图表错误
- Jquery 选项卡不起作用或引用错误
- 使用 JQuery,如何引用同级的第一个选项
- 当我选择选项值时如何引用页面
- 如何在不丢失引用的情况下打开新选项卡
- 模块构建失败:引用错误:[BABEL] 未知选项:base.stage
- j查询选项对象引用
- 我怎样才能重写这个javascript代码,以便它引用每个选项框
- 如何在Baqend Dashboard的Data选项卡中创建示例引用列表?
- 将DOM节点链接到(浏览器内)域对象的选项:是直接引用
- 在一个选项元素中引用多个值
- js引用一个选项值
- 关闭选项卡时Javascript引用循环
- 如何在剑道UI TabStrip中获得对当前选定选项卡的引用
- Jquery UI选项卡在ajax调用中无法引用
- Jquery UI datepicker.对象中的选项.& # 39;这个# 39;不引用对象的内部函数
- JQuery小部件-通过引用传递选项