2015-04-16 62 views
0

问题我有这个JS,由于某些原因,它不能在IE或Chrome中工作,但它在FF中工作,帮助将是不胜感激。似乎无法弄清楚为什么我的JavaScript不工作在IE和Chrome

附注我需要这是一些奇怪的原因点击功能的变化function--在IE和Chrome,但在这种情况下,我需要的是一个变化,这也是一个RubyRails应用。

这里是我的代码..

$(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."); 
       } 
      }); 
     }); 
    }); 

我也试过这仍然没有在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."); 
       } 
      }); 
     }); 
    }); 

想这也仍然在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."); 
       } 
      }); 
     }); 
    }); 

这是我的看法代码

<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> 
+0

什么是不正确的工作?更改事件是否被触发? Ajax调用是否正在进行?处理Ajax调用是否存在错误(又名https://api.jquery.com/ajaxError/)您将需要调试此操作。 – epascarello

+0

该警报是一个自定义警报,我只是使用确认,因为我是懒惰,并希望创建分开警报框大声笑。 @ epascarello – Snowman1234

+0

它似乎是改变功能不工作,因为当我改变它点击它的作品和AJAX的作品,所以我很确定它与改变功能有关。 @epascarello – Snowman1234

回答

0

jQuery的更改事件仅触发,当输入字段失去焦点。

编辑:

$(文件)。就绪(函数(){

$('#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."); 
     } 
    }); 
}); 

编辑#2:我认为,有在我的示例代码中的语法错误......

+0

这因为一旦用户点击了另一盒是正确的还是打标签它的工作原理,但它仅与FF @marcel – Snowman1234

+0

变化工作“什么是不正确的工作?是否触发了更改事件?是否创建了Ajax调用?“ - epascarello – marcel

+0

你可以使用浏览器的调试模式找出问题所在......或者添加一些放置好的console.logs ... – marcel

0

这应该为你工作:

$(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."); 
     } 
    }); 
    }); 
}); 
+0

这工作在IE浏览器和FF,但它只允许我键入一个字母然后它就解雇了Ajax。有没有办法改变? – Snowman1234

+0

@雪人1234:我以为那就是你要去的地方。你几乎必须这样做(每个字符),使用'change'(焦点必须离开输入),或者你可以用定时器做一些更奇特的事情,这会让你在发起AJAX请求之前等待用户暂停。 – jbeck

0

好吧,我想通了......所有我需要做的就是改变这个$(“#EMP_ID”)改变(函数()。 {to this $('#emp_id')。blur(function(){a

相关问题