简体   繁体   English

为什么这个简单的脚本在IE11中不起作用

[英]Why doesn't this simple script work in IE11

I have a issue with an application and I have recreated the same issue by skinning down the code to the source of the problem. 我的应用程序有问题,并且通过将代码缩小为问题的根源来重新创建了相同的问题。 IE11 is returning an error in the console "'test1_1' is undefined" IE11在控制台中返回错误“'test1_1'未定义”

This code works in Firefox so I need to understand why its not working in IE11. 该代码在Firefox中有效,因此我需要了解为什么它在IE11中不起作用。 Hoping its something straight forward any help would be appreciated. 希望它的东西直接任何帮助将不胜感激。

jQuery: jQuery的:

function testAlert(row,defect){
    alert(defect);
};

HTML: HTML:

 <form>
    <div class="input-group">
       <input type="text" id="test1_1" name="test1_1" style="width:150px" readonly="true" value="test">
          <span class="btn btn-default btn-sm input-group-addon" 
                id="customer_search" 
                onclick="testAlert('1',$(test1_1).val());
          ">
             test
           </span>
    </div>
</form>  

Not all browsers set a global variable for elements having an id. 并非所有浏览器都为具有id的元素设置全局变量。 And they don't always do it the same way. 而且他们并不总是以相同的方式做。 Using window.yourNodeId to get an element by its id is thus considered unreliable and a bad practice, you should use a selector here instead : 因此,使用window.yourNodeId通过其ID获取元素是不可靠的,并且是一种不好的做法,您应该在此处使用选择器:

onclick="testAlert('1',$('#test1_1').val());"

Related : Do DOM tree elements with ids become global variables? 相关: 具有ID的DOM树元素是否会成为全局变量?

您忘记了'#'

onclick="testAlert('1',$('#test1_1').val());

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

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