如何禁用if语句按钮

时间:2017-09-28 16:28:59

标签: javascript jquery

如何通过单击某个按钮来禁用按钮?

看起来像这样,如果我单击“接受”按钮(仅禁用“接受”按钮),则启用其他按钮。 order_status = Accepted然后submitAccept按钮将被停用。

但是现在发生的事情是它没有得到order_status(Accepted)的值,即使我在调度中更改了它仍然禁用了submitAccept按钮。

更新:它禁用按钮接受但它禁用所有接受按钮即使order_status被调度或待处理 继承我的代码

<?php 


 <div class="form-group">
 <label for="order_status" class="col-sm-2 control-label">Order Status</label> 
 <div class="col-lg-3">
 <input type="text" input style="width:500px" class="form-control" name="order_status" id="order_status" placeholder="" value="" required="required" readonly>
 </div> 
 </div>

  <?php 
  if(isset($_POST['ORDER_ID'])){  
$order_id = trim(addslashes($_POST['ORDER_ID']));
$sql = "SELECT food_name, special_request, quantity, amount 
FROM cart_tbl
WHERE order_id=$order_id";
$result = mysqli_query(connection2(), $sql);}
?>
<table class="table table-hover table-bordered">
 <thead>
 <tr> 
 <th>Food</th>
 <th>Special Request</th>
 <th>Quantity</th>
 <th>Amount</th> 
 </tr>
 </thead> 
 <?php
if(mysqli_num_rows($result)>0)
{
    while($row = mysqli_fetch_array($result))
    {
?>
<tr>
<td><?php echo $row["food_name"];?></td>
<td><?php echo $row["special_request"];?></td>
<td><?php echo $row["quantity"];?></td>
<td><?php echo $row["amount"];?></td>
</tr>
<?php
    }
}
?>

</table>
 </div>
 <div class="modal-footer">
<button type="submit" input style="background-color:##FF0000;color:white;float:left" name="showFood" id="showFood" class="btn btn-primary " onclick="if(!confirm('Are you sure you want to see food order?')){return false;}" > Show Food</button>
<button type="submit" input style="background-color:#4CAF50;color:white" name="submitDelivered" id="submitDelivered" class="btn btn-primary " onclick="if(!confirm('Are you sure you want to deliver order?')){return false;}" > Delivered </button>
// HERE IT IS\\ <button type="submit" input style="background-color:#0000FF;color:white" name="submitAccept" id="submitAccept" class="btn btn-primary" onclick="if(!confirm('Are you sure you want to accept order?')){return false;}" <?php if($_POST['$order_status']="Accepted"): ?> disabled <?php endif ?> > Accept </button>
 <button type="button" style="background-color:#FFFF00;color:black" class="btn btn-success" data-toggle="modal" data-target="#myDropdown"> Send </button> 
 <button type="submit" input style="background-color:#f44336;color:white" name="submitCancel" class="btn btn-danger" onclick="if(!confirm('Are you sure you want to cancel order?')){return false;}">Cancel</button> 
  <?php
if(isset($_POST['submitDelivered'])){  
  $order_id = trim(addslashes($_POST['ORDER_ID']));
  $driver_number= trim(addslashes($_POST['drivers_number']));
  $query = "UPDATE order_tbl SET `order_status`='Delivered' WHERE `order_id` = $order_id";
  if (mysqli_query(connection2(), $query)) { 
         mysqli_query(connection2(), "COMMIT");
         $_SESSION['message'] = "Order Delivered"; } 
         else { 
         $_SESSION['message'] = mysqli_error(connection2());
         mysqli_query(connection2(), "ROLLBACK");
         }

  if($_POST['drivers_number']!=0){      
  $query2 = "UPDATE driver_tbl SET `order_id`='' WHERE `driver_number` = $driver_number";
  if (mysqli_query(connection2(), $query2)) { 
         mysqli_query(connection2(), "COMMIT");
         }
    }
  }

  if(isset($_POST['submitAccept'])){  
  $order_id = trim(addslashes($_POST['ORDER_ID']));
  $driver_number= trim(addslashes($_POST['drivers_number']));
  $query = "UPDATE order_tbl SET `order_status`='Accepted' WHERE `order_id` = $order_id";
  if (mysqli_query(connection2(), $query)) { 
         mysqli_query(connection2(), "COMMIT");
         $_SESSION['message'] = "Order Accepted"; } 
         else { 
         $_SESSION['message'] = mysqli_error(connection2());
         mysqli_query(connection2(), "ROLLBACK");
         }

  if($_POST['drivers_number']!=0){      
  $query2 = "UPDATE driver_tbl SET `order_id`='' WHERE `driver_number` = $driver_number";
  if (mysqli_query(connection2(), $query2)) { 
         mysqli_query(connection2(), "COMMIT");
         }
    }
  }


  if(isset($_POST['submitCancel'])){  
  $order_id = trim(addslashes($_POST['ORDER_ID']));
  $driver_number= trim(addslashes($_POST['drivers_number']));
  $query = "UPDATE order_tbl SET `order_status`='Cancelled' WHERE `order_id` = $order_id";
  if (mysqli_query(connection2(), $query)) { 
         mysqli_query(connection2(), "COMMIT");
         $_SESSION['message'] = "Order Cancelled"; } 
         else { 
         $_SESSION['message'] = mysqli_error(connection2());
         mysqli_query(connection2(), "ROLLBACK");
         }

  if($_POST['drivers_number']!=0){      
  $query2 = "UPDATE driver_tbl SET `order_id`='' WHERE `driver_number` = $driver_number";
  if (mysqli_query(connection2(), $query2)) { 
         mysqli_query(connection2(), "COMMIT");
         }

    }
  }

  ?>

 </div>
 </form>
 </div>
 </div>
 </div>

我也试过这段代码,但没有发生。

<script type='text/javascript'>
  $(function () {
      ($order_status).keyup(function (){
          if ($order_status == 'Accepted') {
              ('.submitAccept').prop('disabled',true);
          }else{
              ('.submitAccept').prop('disabled', false);
          }
  })
  };
  );

  </script>

2 个答案:

答案 0 :(得分:0)

您的JS代码不正确。我也不确定如何填充order_status值,因为它是readonly字段。

 $(function () {
  ($("#order_status").keyup(function (){
      if ($("#order_status").val() == 'Accepted') {
          ('.submitAccept').prop('disabled',true);
      }else{
          ('.submitAccept').prop('disabled', false);
      }
  })
 };
);

答案 1 :(得分:0)

代码包含多个错误,包括HTML错误,例如:

<input type="text" input style="width:500px" 
class="form-control" name="order_status" 
id="order_status" placeholder="" value="" 
required="required" readonly>

要指定“order_status”是输入的名称,您只需要在输入标记中输入一次代码“input”而不是两次。

JavaScript也需要更正。要引用HTML元素“order_status”,在jQuery中正确的语法是$(“#order_status”)。并且,还有另一个元素“sumbitAccept”,在jQuery中你称为$("#submitAccept").我还添加了一些代码来更新readonly输入。

function doAccepted() {
  $('#order_status').val('Accepted');
  $('#submitAccept').prop('disabled',true).css({"background-color":"#ccc"});
  if(!confirm('Are you sure you want to accept order?')){
   $('#order_status').val('Accepted').val("");
  $('#submitAccept').prop('disabled',false).css({"background-color":"#00f"});
  return false;
  } 
  else 
  {
       return true;
  }    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button type="submit" style="background-color:#0000FF;color:white" name="submitAccept" id="submitAccept" class="btn btn-primary" onclick="doAccepted()"> Accept </button>

<div class="form-group">
 <label for="order_status" class="col-sm-2 control-label">Order Status</label> 
 <div class="col-lg-3">
 <input type="text"  style="width:500px" class="form-control" name="order_status"  id="order_status" placeholder="" value="" required="required" readonly>
 </div> 
 </div>

// error in the PHP:  
<?php 
 if($_POST['$order_status']="Accepted"): ?> disabled <?php endif ?> > 
 Accept </button>

将“=”替换为“==”以进行相等性测试 - 这是一个常见错误,如果您按照这种格式重写代码,则可以避免:

<?php if("Accepted" == $_POST['$order_status']): ?> 

那说PHP是一个等待发生的问题。您必须始终将提交的表单值视为受污染,并在代码中使用之前验证和/或过滤

最后,仅仅因为你只是一个表单元素readonly并不意味着它是安全的。任何具有恶意意图的人都可以欺骗该表单并删除readonly属性并提交表单。

还有一件事,虽然PHP最初是因为你可以在PHP代码中嵌入PHP而引起了人气,但这并不一定是最好的主意。在模型 - 视图 - 控制器模型(即MVC)之后分离关注点要好得多。您会发现调试更容易,您的代码和架构将更加强大。