onchange事件不起作用

时间:2018-07-28 21:46:53

标签: javascript html dom

onchange事件在以下代码中不起作用。我有一个选择标签,如果选择了“是”,我想显示一个文本框。当我选择“是”时,该文本框不会出现。

代码:

private static IScheduler MakeScheduler(IContext ctx) {
    ... // the code from your lambda goes here
}
...
ForSingletonOf<IScheduler>().Use(ctx => MakeScheduler(ctx));

2 个答案:

答案 0 :(得分:1)

选项Yes的值实际上是"1",而不是"Yes"

<script type = "text/javascript">
            function showfield(name) {
                if (name == "1") {
                    document.getElementById("div1").innerHTML = '<input type = "text" cols = "10" rows = "9" name = "yes" />';
                } else {
                    document.getElementById("div1").innerHTML = "";
                }
            }
</script>

<select name = "Prior_Knowledge" class = "user" id = "prior_knowledge" onchange = "showfield(this.options[this.selectedIndex].value);">
            <option> </option>
            <option value = "1">Yes</option>
            <option value = "2">No</option>
</select>
<div class = "user" id = "div1"></div>

答案 1 :(得分:0)

尝试一下

var priorKnowledge =  document.getElementById("prior_knowledge");

priorKnowledge.addEventListener("change", function() {
    if(this.options[this.selectedIndex].text == "Yes"){
      document.getElementById("div1").innerHTML = '<input type = "text" cols = "10" rows = "9" name = "yes" />';
    }else{
    document.getElementById("div1").innerHTML = "";
    }
});
<select name = "Prior_Knowledge" class = "user" id ="prior_knowledge">
  <option> </option>
  <option value = "1">Yes</option>
  <option value = "2">No</option>
</select>
<div class = "user" id = "div1"></div>

或者您的原始代码

<script type = "text/javascript">
            function showfield(name) {
                if (name == "Yes") {
                    document.getElementById("div1").innerHTML = '<input type = "text" cols = "10" rows = "9" name = "yes" />';
                } else {
                    document.getElementById("div1").innerHTML = "";
                }
            }
</script>

<select name = "Prior_Knowledge" class = "user" id = "prior_knowledge" onchange = "showfield(this.options[this.selectedIndex].text);">
            <option> </option>
            <option value = "1">Yes</option>
            <option value = "2">No</option>
</select>
<div class = "user" id = "div1"></div>

更改this.options[this.selectedIndex].value中的this.options[this.selectedIndex].text