簡體   English   中英

如何在ajax成功中調用javascript函數?

[英]How to call a javascript function inside ajax success?

我想在單擊驗證按鈕以及數據值有效時激活更新按鈕。 一旦數據值有效,更新按鈕就會起作用。

這是模式中按鈕的 html 代碼

<div class="modal-footer">
        <button type="button" class="btn btn-primary validatebtn" id="btn_validate" onclick="activateupdate()">Validate</button>
        <button type="button" class="btn btn-primary causeUpdatebtn" id="btn_update" disabled="disabled">Update</button>

這是 AJAX 代碼。 我正在從模態中提取值。 它使輸出有效,但更新按鈕仍未啟用。

我應該做哪些更改以便調用 activateupdate() 函數

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

})

$(document).on('click','.validatebtn', function(){
   let ValidId = $('#span_id').text();
   let ValidDesc = $('#Desc').val();

   let causeObj = new Object();
   causeObj.causeId = ValidId;
   causeObj.causeDescription = ValidDesc;

   let ValidFinalObj = new Object();

   $.ajax({
       type: 'POST',
       url: 'https://.../validate_apicall',  (taking an example)
       contentType: 'application/json',
       data: JSON.stringify(ValidFinalObj),
       success: function (data) {
           if(data.indexOf('Failed') > -1){
               pwIsf.alert({msg:'Not valid',type:'error'});   
           }
           else {
               pwIsf.alert({msg:'Valid',type:'info'}); 

               activateupdate()
               {
                   document.getElementById('btn_update').removeAttribute('disabled')
               } 
           }
       },
       error: function (xhr, status, statusText) {            
           console.log("Failed to Validate");
       }
   })   
}
); 

為什么要在代碼中使用函數語法?! 只需將您的代碼更改為此

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

})

$(document).on('click','.validatebtn', function(){
   let ValidId = $('#span_id').text();
   let ValidDesc = $('#Desc').val();

   let causeObj = new Object();
   causeObj.causeId = ValidId;
   causeObj.causeDescription = ValidDesc;

   let ValidFinalObj = new Object();

   $.ajax({
       type: 'POST',
       url: 'https://.../validate_apicall',  (taking an example)
       contentType: 'application/json',
       data: JSON.stringify(ValidFinalObj),
       success: function (data) {
           if(data.indexOf('Failed') > -1){
               pwIsf.alert({msg:'Not valid',type:'error'});   
           }
           else {
               pwIsf.alert({msg:'Valid',type:'info'}); 

               document.getElementById('btn_update').removeAttribute('disabled') // <- HERE
           }
       },
       error: function (xhr, status, statusText) {            
           console.log("Failed to Validate");
       }
   })   
}
); 

問題是因為您已將啟用按鈕的邏輯置於您從未調用過的(語法上不正確的)函數中。 從您想要實現的描述中,您只需刪除該塊即可。

首先從 HTML 中刪除onclick屬性。 它們是不好的做法,無論如何這里都不需要。

<div class="modal-footer">
  <button type="button" class="btn btn-primary validatebtn" id="btn_validate">Validate</button>
  <button type="button" class="btn btn-primary causeUpdatebtn" id="btn_update" disabled="disabled">Update</button>

然后從success處理函數中刪除有問題的塊:

success: function(data) {
  if (data.indexOf('Failed') > -1) {
    pwIsf.alert({
      msg: 'Not valid',
      type: 'error'
    });
  } else {
    pwIsf.alert({
      msg: 'Valid',
      type: 'info'
    });

    $('#btn_update').prop('disabled', false)
  }
},

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM