简体   繁体   English

在 Ajax 成功后运行 JavaScript function

[英]Run JavaScript function after Ajax success

I have two functions that I want to run after the Ajax call succeeds but only one work.在 Ajax 调用成功后,我有两个要运行的函数,但只有一个工作。

The Javascript code is working if I put it outside the Ajax call and it worked as an onclik.如果我将 Javascript 代码放在 Ajax 调用之外并且它作为一个 onclik 工作,它就可以工作。 Basically, I click an element and fill the value of the input of this element in another.基本上,我单击一个元素并将该元素的输入值填充到另一个元素中。 But now I want to fill the value only after the call succeeds.但是现在我只想在调用成功后才填充值。

The original code:原代码:

$('*[id^="newbudbtn-"]').on('click', showMessage); //This is the line I want to be executed after the Ajax call

function showMessage(event) {
    var elem = $(event.target);
    var id = elem.attr('id').replace('newbudbtn-', '');

    var source = $('#newbud-' + id)
    var target = $('#bud-' + id);

    if (source.length && target.length) {
      target.text('$' + source.val());
    }
}

This is the Ajax call.这是 Ajax 调用。 "myAlertTop();" “我的警报顶部();” is working, but I can't get "showMessage()" to do the same.正在工作,但我不能让“showMessage()”做同样的事情。

$("a.bgbtb").click(function(){
  var btnid = $(this).attr("id").split('newbudbtn-')[1];
  var newbudget = $("INPUT[id=newbud-"+btnid+"]").val();
  var platform = $("span#"+btnid).text(); 
  $.ajax({
    url:"campbdgtedit.php",
    method:"POST",  
    data:{platform:platform, btnid:btnid, newbudget:newbudget}, 
    success:function(data){
        myAlertTop();
        $('*[id^="newbudbtn-"]').on('click', showMessage);

    }
  });
});

I understand that the onlick won't work, so I tried desperately onload and also calling the function directly, but nothing seems to work.我知道 onlick 不起作用,所以我拼命尝试加载并直接调用 function,但似乎没有任何效果。 I also tried echoing the JS inside campbdgtedit.php, but it didn't work too.我还尝试在 campbdgtedit.php 中回显 JS,但它也没有用。

The major issue is that there are many clickable elements, one on each row of a table, and only the element on the same row should be changed.主要问题是有很多可点击的元素,一个表格的每一行都有一个,并且只有同一行的元素应该被改变。

Do you have a solution to this problem?你有解决这个问题的办法吗?

You should try using fetch and then.您应该尝试使用 fetch 然后。 As soon as the request is received and the response is given, it executes the 'then(callback)'.一旦收到请求并给出响应,它就会执行“then(callback)”。 That way you could call your function and pass the received data.这样你就可以调用你的 function 并传递接收到的数据。

fetch(url, {request config}).then((response) => {

 /*write code and use the data */ 
});

From what I understand you can update your showMessage() to accept the element itself instead of an event.据我了解,您可以更新您的showMessage()以接受元素本身而不是事件。

updated showMessage更新showMessage

function showMessage(elem) {   
    var id = elem.getAttribute('id').replace('newbudbtn-', '');

    var source = '#newbud-' + id;
    var target = '#bud-' + id;

    console.log(source);
    console.log(target);
}

And now you can call this method directly after ajax success, so your ajax success method will look something like现在你可以在 ajax 成功后直接调用这个方法,所以你的 ajax 成功方法看起来像

$("a.bgbtb").click(function(){
  const _btn = this;
  var btnid = $(this).attr("id").split('newbudbtn-')[1];
  var newbudget = $("INPUT[id=newbud-"+btnid+"]").val();
  var platform = $("span#"+btnid).text(); 
  $.ajax({
    url:"campbdgtedit.php",
    method:"POST",  
    data:{platform:platform, btnid:btnid, newbudget:newbudget}, 
    success:function(data){
        myAlertTop();
        showMessage(_btn);
    }
  });
});

jsfiddle link for reference https://jsfiddle.net/4ahef7mt/2/ hope this helps. jsfiddle 链接供参考https://jsfiddle.net/4ahef7mt/2/希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM