简体   繁体   中英

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"

This code works in Firefox so I need to understand why its not working in IE11. Hoping its something straight forward any help would be appreciated.

jQuery:

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

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. 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 :

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

Related : Do DOM tree elements with ids become global variables?

您忘记了'#'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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