【问题标题】:How to call a javascript function inside ajax success?如何在ajax成功中调用javascript函数?
【发布时间】:2020-02-13 10:59:57
【问题描述】:

我想在单击验证按钮后以及数据值有效时激活更新按钮。一旦数据值有效,更新按钮就会起作用。

这是模态框内按钮的 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");
       }
   })   
}
); 

【问题讨论】:

    标签: javascript jquery ajax bootstrap-modal ajaxform


    【解决方案1】:

    为什么在代码中使用函数语法?!只需将您的代码更改为此

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

    【讨论】:

      【解决方案2】:

      问题是因为您已将启用按钮的逻辑置于您从未调用过的(语法不正确的)函数中。从您想要实现的目标的描述中,您只需删除该块。

      首先从 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)
        }
      },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-25
        • 2012-07-27
        • 1970-01-01
        • 1970-01-01
        • 2020-07-04
        • 2013-08-04
        • 2016-10-07
        • 1970-01-01
        相关资源
        最近更新 更多