如何在 AJAX 中成功后更改按钮文本


how to change button text after succes in ajax

我想在成功时更改按钮文本。我想在成功后将其更改为接受

    <button type="button" onclick="saveData<?php echo $row1->id; ?>()">Accept</button> 
      <script>
      function saveData<?php echo $rrr->id; ?>(){
      $.ajax({
        type: "POST",
        url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
        data:{},       
        success:function( data )
        {
        }
       });
  }

我的函数正常工作,因为我在成功后使用了警报,但我不知道如何在成功后修改其文本。请帮助

您可以将类与以下按钮关联:

<button type="button" onclick=".." class="submit-btn">Accept</button> 

现在,在 AJAX 成功代码中,提及以下内容:

 $.ajax({
    type: "POST",
    url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
    data:{},       
    success:function( data ) {
       $(".submit-btn").html("Accepted");   // Add this line
    }
});
$.ajax({
  type: "POST",
  url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
  data: {},
  success: function(data) {
   $("#btn").text('your new text here'); // add id to your button
  }
});
<button id="btn" type="button" onclick="saveData<?php echo $row1->id; ?>()">Accept</button> 

1) 在函数中传递this

2)使用.text()功能更改按钮的文本。

<button type="button" onclick="saveData<?php echo $row1->id; ?>(this)">Accept</button> 
<script>
          function saveData<?php echo $rrr->id; ?>(obj){
              $.ajax({
                 type: "POST",
                 url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
                 data:{},       
                 success:function( data )
                 {
                      $(obj).text("New Text");
                 }
              });
          }
</script>

HTML : 在按钮中添加 id

<button type="button" id="accept" onclick="saveData<?php echo $row1->id; ?>()">Accept</button> 

脚本:

 success:function( data ) {
       $("#accept").html("accepted");   
    }

id; ?>()">接受 函数 saveDataid; ?>(){

  $.ajax({
    type: "POST",
    url: "<?php echo base_url().'home/accept_seller/'. $rrr->id; ?>",
    data:{},       
    success:function( data )
    {
       $("#buttonid").html('Changetext');
    }
   });