简体   繁体   English

按ID获取元素不起作用

[英]Get element by id doesn't work

So when my value is gsm i just need an alert with ("nice it works") but it doesn't want to work! 因此,当我的值为gsm时,我只需要发出一个警报(“它很好用”),但它不希望起作用! Can you guys help me out? 你们能帮我吗? Without changing to much of my code? 无需更改大部分代码? Unless it's really wrong ofcourse. 除非这确实是错误的。

This is my Html: 这是我的HTML:

<select id="telefoonkeuze" onchange="Checkfunction()">
        <option value="gsm" id="gsm">gsm</option>
        <option value="telefoon">telefoon</option>
</select>

This is my js: 这是我的js:

        var gsm = document.getElementById("gsm");

        function Checkfunction() {
            var x = document.getElementById("telefoonkeuze").value;
            if (x == gsm) {
                alert("nice it works!");
            }
        }

when you do this 当你这样做

var gsm = document.getElementById("gsm");

you are assigning an element to your gsm variable. 您正在为gsm变量分配一个元素。 Then later comparing that element to a value. 然后,将该元素与一个值进行比较。

just change that line 只是改变那条线

var gsm='gsm';

or elimate the variable and put the 'gsm' string in your if statement. 或消除该变量,然后将'gsm'字符串放入if语句中。

also you may have trouble getting the selected value as you are. 同样,您可能无法按原样获得所选值。 there is another thread about that though. 不过,还有另一个话题。 Get selected value in dropdown list using JavaScript? 获取使用JavaScript在下拉列表中选择的值?

 var Checkfunction = function() { var x = document.getElementById("telefoonkeuze"); if (x.value == "gsm") { alert("here you go"); } } 
 <select id="telefoonkeuze" onchange="Checkfunction()"> <option value="gsm" id="gsm">gsm</option> <option value="telefoon">telefoon</option> </select> 

You can do it this. 你可以做到这一点。 http://jsfiddle.net/c8go6663/2/ http://jsfiddle.net/c8go6663/2/

 var gsm = document.getElementById("gsm").value;

        Checkfunction = function() {
            var x = document.getElementById("telefoonkeuze").value;
            if (x == gsm) {
                alert("nice it works!");
            }
        }

The following section is a pure JS modification of your code. 下一节是对代码的纯JS修改。 You may view it in this fiddle too (adapted to the jsfiddle environment): 您也可以在这个小提琴中查看它(适应jsfiddle环境):

<html>
  <head>
    <title>getElementById fails</title>
    <script type="text/javascript">
        var gsm;

        function Checkfunction() {
            var x = document.getElementById("telefoonkeuze").value;
            if (x === gsm) {
                alert("nice it works!");
            }
        }

        window.addEventListener ( "load", function () { gsm = document.getElementById('gsm').value; 1; });
    </script>
  </head>
  <body>
      <div>
          <select id="telefoonkeuze" onchange="Checkfunction()">
                  <option value="gsm" id="gsm">gsm</option>
                  <option value="telefoon">telefoon</option>
          </select>
     </div>
  </body>
</html>

Note 注意

This answer is supposed to be robust and working. 该答案应该是可靠且有效的。 Credits (and acceptance) should still be bestowed on @RightClick for he pinpointed the definite flaw in the original code (which I, shame on me, at first did not spot...). @RightClick仍应给予荣誉(和接受),因为他指出了原始代码中的明确缺陷(我感到羞耻,起初没有发现...)。

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

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