简体   繁体   English

似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行

[英]Can't seem to figure out why my javascript isn't working in IE and Chrome

Question I have this js and for some reason it isn't working in IE or Chrome but, it does work in FF and help would be greatly appreciated. 问题我有这个js,由于某种原因它不能在IE或Chrome中使用,但是它确实可以在FF中使用,帮助将非常感谢。

Side note I need this to be a change function-- for some strange reason click function works in IE and Chrome but, in this situation I need it to be a change, also this a RubyRails application. 旁注,我需要将其用作更改功能-出于某些奇怪的原因,单击功能可在IE和Chrome中工作,但是在这种情况下,我需要将其更改为RubyRails应用程序。

Here is my code .. 这是我的代码..

$(document).ready(function () {

    $('#emp_id').change(function () {
       var url = "/user/populate_form?emp_id=" + $(this).val();
       $.getJSON(url, function (data) {
            if (!(data.emp_first_name === undefined))
                $('#emp_first_name').val(data.emp_first_name);
            if (!(data.emp_last_name === undefined))
                $('#emp_last_name').val(data.emp_last_name);
            if ((data.error * 1) == 404) {
               alert("The employee ID entered was not found!");
               } else {
               window.confirm("Your employee ID was found please fill in the email(optional) and password fields then click the sign in button to register.");
               }
           });
       });
   });

I also tried this still didn't work in IE & Chrome 我也尝试过在IE和Chrome浏览器中仍然无法使用

$(document).ready(function () {

    $('#emp_id').on('change', function () {
       var url = "/user/populate_form?emp_id=" + $(this).val();
       $.getJSON(url, function (data) {
            if (!(data.emp_first_name === undefined))
                $('#emp_first_name').val(data.emp_first_name);
            if (!(data.emp_last_name === undefined))
                $('#emp_last_name').val(data.emp_last_name);
            if ((data.error * 1) == 404) {
               alert("The employee ID entered was not found!");
               } else {
               window.confirm("Your employee ID was found please fill in the email(optional) and password fields then click the sign in button to register.");
               }
           });
       });
   });

Tried this as well still did not work in IE and Chrome 也尝试过这一点,但在IE和Chrome中仍然不起作用

$(document).ready(function () {

    $('#emp_id').keyup('onchange', function () {
       var url = "/user/populate_form?emp_id=" + $(this).val();
       $.getJSON(url, function (data) {
            if (!(data.emp_first_name === undefined))
                $('#emp_first_name').val(data.emp_first_name);
            if (!(data.emp_last_name === undefined))
                $('#emp_last_name').val(data.emp_last_name);
            if ((data.error * 1) == 404) {
               alert("The employee ID entered was not found!");
               } else {
               window.confirm("Your employee ID was found please fill in the email(optional) and password fields then click the sign in button to register.");
               }
           });
       });
   });

Here is my view code 这是我的查看代码

    <div class='row form-group'>
      <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
       <%= f.text_field :emp_id, tabindex: 1, id: 'emp_id', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
      </div>
    </div>

    <div class='row form-group'>
      <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
       <%= f.text_field :emp_first_name, tabindex: 1, id: 'emp_first_name', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
      </div>
    </div>

    <div class='row form-group'>
      <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
       <%= f.text_field :emp_last_name, tabindex: 1, id: 'emp_last_name', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
      </div>
    </div>

The jQuery Change event only triggers, when the input field loses focus. jQuery Change事件仅在输入字段失去焦点时触发。

EDIT: 编辑:

$(document).ready(function () { $(document).ready(function(){

$('#emp_id').change(function () {
   var url = "/user/populate_form?emp_id=" + $(this).val();

   $.getJSON(url, function (data) {
        data = JSON.parse(data);

        if (data.emp_first_name !== undefined) {
            $('#emp_first_name').val(data.emp_first_name);
        }

        if (data.emp_last_name !== undefined) {
            $('#emp_last_name').val(data.emp_last_name);
        }

        if (parseInt(data.error) === 404) {
           alert("The employee ID entered was not found!");
        } else {
           window.confirm("Your employee ID was found please fill in the email(optional) and password fields then click the sign in button to register.");
        }
   });
});

EDIT #2: I think, there was a syntax error in my example code... 编辑2:我认为示例代码中存在语法错误...

This should work for you: 这应该为您工作:

$(document).ready(function() {
  $('#emp_id').keyup(function() {
    var url = "/user/populate_form?emp_id=" + $(this).val();
    $.getJSON(url, function (data) {
      if (typeof(data.emp_first_name) !== 'undefined')
        $('#emp_first_name').val(data.emp_first_name);

      if (typedof(data.emp_last_name) !== 'undefined')
        $('#emp_last_name').val(data.emp_last_name);

      if (parseInt(data.error) == 404) {
        alert("The employee ID entered was not found!");
      }
      else {
        window.confirm("Your employee ID was found please fill in the email(optional) and password fields then click the sign in button to register.");
      }
    });
  });
});

好吧,我知道了...我要做的就是将$('#emp_id')。change(function(){更改为$('#emp_id')。blur(function(){

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

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