我无法通过jquery更改按钮的状态

时间:2016-09-20 15:39:43

标签: php jquery ajax

这是我在前端的以下代码:

                             <div class="panel panel-primary">
                                <div class="panel-heading text-center text-uppercase">Birth Certificates For Overall (Pending, Completed)</div>
                                <div class="panel-body">
                                <div class="box-body">
                                  <table id="viewer" class="table table-bordered">
                                    <thead>
                                      <tr>
                                        <th>Sr No</th>
                                        <th>Reg Number</th>
                                        <th>From Hospital</th>
                                        <th>Actions</th>
                                      </tr>
                                    </thead>
                                    <tbody>
                                        <?php $Viewer->showAllData(); ?>
                                    </tbody>
                                    <tfoot>
                                      <tr>
                                        <th>Sr No</th>
                                        <th>Reg Number</th>
                                        <th>From Hospital</th>
                                        <th>Actions</th>
                                      </tr>
                                    </tfoot>
                                  </table>
                                </div><!-- /.box-body -->
                              </div><!-- /.box -->
                            </div>

这是我在其中调用对象的类代码:

    public function showAllData()
                {
                    $query = "SELECT * FROM certificate_details ORDER BY created DESC";
                    $connection = $this->establish_connection();
                    $details = $connection->query($query);
                    $connection->close();

                    if($details->num_rows > 0)
                        {
                            $counter = 1;
                            while($detail = $details->fetch_assoc())
                                {
                                    $status = $detail['status'];

                                    if($status == 0)
                                        {
                                            $bg = "bg-danger";
                                            $issuestatus = "btn btn-success";
                                            $message = "Confirm Issue!";
                                        }
                                    elseif($status == 1)
                                        {
                                            $bg = "bg-success";
                                            $issuestatus = "btn btn-success disabled";
                                            $message = "Certificate Issued";
                                        }
                                    else
                                        {
                                            $bg = "bg-warning";
                                            $issuestatus = "btn btn-warning";
                                        }

                                    echo "
                                            <tr class='odd gradeX ".$bg."'>
                                                <td>".$counter."</td>
                                                <td>".$detail['registration_number']."</td>
                                                <td>".$this->getHospitalInfo($detail['user_id'])."</td>
                                                <td style='margin: 0;'><div class='btn btn-primary' href='#' value='".$detail['id']."' id='view-details'>View Details</div><div style='margin-left: 10px;' class='".$issuestatus."' href='#' value='".$detail['id']."' id='confirm-issue'>".$message."</div></td>
                                            </tr>    
                                         ";
                                    $counter = $counter + 1;
                                }
                        }
                }

我给按钮指定了一个特定的类,即class =&#34; confirm-issue&#34;并且在值部分中它保存唯一值

这是我在调用(&#34;#confirm-issue&#34;)时触发的以下JQuery代码。单击(function(){})

      $("body").on("click", ".confirm-issue",function() 
        {
            var certificateId = $(this).attr('value');

            if (confirm('Are You Sure You Want To Confirm The Issue of Certificate? If Once Confirmed Can\'t be Undone.\n\t\t\t\t\tPlease Confirm That You Have Issued The Certificate.'))
                {
                    $.ajax({
                                url: 'get_data.php?id=setCertificateDetailsIssued',
                                type: 'POST',
                                dataType: 'html',
                                data: {certificate_id : certificateId},
                            })
                    .done(function(resp)
                        {
                            console.log(resp);
                            if(resp == 1)
                                {
                                    var data = "Certificate Issued Succesfully!";
                                    $(this).attr('value').val("Certificate Isssued").attr('disabled', 'true');
                                    $('#message').html("");
                                    $('#message').html(data);
                                    $('#messageModal h3.modal-title').html('');
                                    $('#messageModal h3.modal-title').html('Certificate Issued Successfully!');
                                    $('#messageModal').modal('show');
                                }

                            else if(resp == 0)
                                {
                                    var data = "There Was a Technical Error While Setting The Certificate Status To Issued. Please Try After Some Time Or Try Reloading The Page Once Again!";
                                    $('#message').html("");
                                    $('#message').html(data);
                                    $('#messageModal h3.modal-title').html('');
                                    $('#messageModal h3.modal-title').html('Technical Issue!');
                                    $('#viewer tr.gradeX').removeClass('bg-danger');
                                    $('#messageModal').modal('show');
                                }
                        })
                    .fail(function()
                        {
                            console.log("error");
                        });
                }
            else
                {
                    var data = "Please Confirm That You Have Issued The Certificate & Then Click Confirm Issue Again!";
                    $('#message').html("");
                    $('#message').html(data);
                    $('#messageModal h3.modal-title').html('');
                    $('#messageModal h3.modal-title').html('Confirmation Prompt');
                    $('#messageModal').modal('show');
                }
        });

当我点击发行证书按钮时出现问题。值将转到数据库,正在更改,并且resp代码为1,但是当控件转到函数时,如果满足条件,如提供的js代码所示,则它不会执行特定的用于禁用按钮的JQuery代码。

enter image description here

3 个答案:

答案 0 :(得分:1)

你的问题在于你误以为this仍然指的是按钮。它没有。您需要使用另一个var来引用该按钮。

var that = this;
if (confirm('Are You Sure You Want To Confirm The Issue of Certificate? .....'))
{
    $.ajax({
         url: 'get_data.php?id=setCertificateDetailsIssued',
         type: 'POST',
         dataType: 'html',
         data: {certificate_id : certificateId}
     })
      .done(function(resp)
       {
           console.log(resp);
           if(resp == 1)
           {
               var data = "Certificate Issued Succesfully!";
                $(that).attr('value', "Certificate isssued").attr('disabled', 'true');
            }
        });

答案 1 :(得分:0)

这条线看起来不正确:

FIND_IN_SET('red',colors)

您正在尝试按属性获取值,然后在其上调用val函数。尝试将其更改为:

$(this).attr('value').val("Certificate Isssued").attr('disabled', 'true');

答案 2 :(得分:0)

你的语法不正确......

$(this).attr('value').val("Certificate Isssued").attr('disabled', 'true');

而不是......

$(this).val("Certificate Isssued");
$(this).attr('disabled', 'true');
相关问题