2017-08-14 55 views
1

我想通過點擊一個按鈕來調用一個循環內的ajax。 請在下面找到我的代碼。代碼工作在Mozilla Firefox沒有任何問題。但在Google Chrome步驟1步驟2,沒有得到執行,它直接進入每個方法。 我的代碼:兩個jQuery指令在Firefox中工作,而不是在Chrome中

$('#button_id').click(function() { 
 
     $(this).prop("disabled", true); /*Step 1*/ 
 
     $(this).html('<li class="fa fa-refresh fa-spin"></li>&nbsp;&nbsp;Please wait..'); /*Step 2*/ 
 
     $.each(MyData, function (i, value) { 
 
      $.ajax({ 
 
       url: "myurl", 
 
       type: "POST", 
 
       dataType: "json", 
 
       async: false, 
 
       success: function (result) { 
 
        alert("success"); 
 
       }, 
 
       error: function (xhr, status, error) { 
 
        alert("Error"); 
 
       } 
 
      }); 
 
     }); 
 
     $(this).html('<li class="fa fa-Send"></li>&nbsp;&nbsp;Submit'); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button type="button" id="button_id" class="btn btn-lg btn-success warning_1 btn-block" style="border-radius: 3px;"><li class="fa fa-send" ></li>&nbsp;&nbsp;Submit</button>

+2

而不是'$( '本')'使用'$(本)'。刪除單引號。 –

+0

我試圖在Chrome運行你的代碼,並沒有得到在臺階上的任何問題 –

回答

0

您需要使用$(this)而不是$('this')。單引號應刪除:

$('#button_id').click(function() { 
 
    /*Step 1*/ 
 
    $(this).prop("disabled", true); 
 
    /*Step 2*/ 
 
    $(this).html('<li class="fa fa-refresh fa-spin"></li>&nbsp;&nbsp;Please wait..'); 
 
    $.each(MyData, function(i, value) { 
 
    $.ajax({ 
 
     url: "myurl", 
 
     type: "POST", 
 
     dataType: "json", 
 
     async: false, 
 
     success: function(result) { 
 
     alert("success"); 
 
     }, 
 
     error: function(xhr, status, error) { 
 
     alert("Error"); 
 
     } 
 
    }); 
 
    }); 
 

 
    $(this).html('<li class="fa fa-Send"></li>&nbsp;&nbsp;Submit'); 
 

 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id="button_id">Button</button>

+0

是的,我試過還是同樣的問題.. – ansh

+0

請分享相關的HTML太 –

+0

我的HTML代碼: '<按鈕類型=「按鈕」 id =「button_id」class =「btn btn-lg btn-success warning_1 btn-block」style =「border-radius:3px;」>

  •    提交' – ansh

    相關問題