簡體   English   中英

似乎無法弄清楚為什么我的JavaScript無法在IE和Chrome中運行

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

問題我有這個js,由於某種原因它不能在IE或Chrome中使用,但是它確實可以在FF中使用,幫助將非常感謝。

旁注,我需要將其用作更改功能-出於某些奇怪的原因,單擊功能可在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>

jQuery Change事件僅在輸入字段失去焦點時觸發。

編輯:

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

編輯2:我認為示例代碼中存在語法錯誤...

這應該為您工作:

$(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