简体   繁体   English

如何在ajax成功中调用javascript函数?

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

I want to activate the update button once the validate button is clicked and also when the data values are valid.我想在单击验证按钮以及数据值有效时激活更新按钮。 Once the data values are valid the update button functions.一旦数据值有效,更新按钮就会起作用。

This is the html code for the buttons inside a modal这是模式中按钮的 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>

Here is the AJAX code.这是 AJAX 代码。 I am extracting the values from the modal.我正在从模态中提取值。 It gives the output as valid but the update button still is not enabled.它使输出有效,但更新按钮仍未启用。

What changes should i make so that the activateupdate() function is called我应该做哪些更改以便调用 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");
       }
   })   
}
); 

Why you use a function syntax inside your code?!为什么要在代码中使用函数语法?! Just change your code to this只需将您的代码更改为此

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

The issue is because you've put the logic which enables the button in a (syntactically incorrect) function which you never call.问题是因为您已将启用按钮的逻辑置于您从未调用过的(语法上不正确的)函数中。 From the description of what you want to achieve you simply need to remove that block.从您想要实现的描述中,您只需删除该块即可。

Firstly remove the onclick attribute form the HTML.首先从 HTML 中删除onclick属性。 They are bad practice and it's not needed here anyway.它们是不好的做法,无论如何这里都不需要。

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

Then remove the offending block from your success handler function:然后从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