繁体   English   中英

我该如何重写此javascript代码,以便它将引用每个optionbox?

[英]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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM