简体   繁体   English

如何禁用1个按钮却保持其他按钮处于启用状态

[英]How to Disabled 1 button yet keep others enabled

When the $order_status is Accepted only the button containing Accept is disabled. $order_status接受时,仅禁用包含接受的按钮。 And If $order_status is Dispatched the disabled button is sent. 如果$order_status调度 ,则发送禁用按钮。 And if $order_status is Cancelled the disabled button is Cancel. 如果$order_status取消,则禁用按钮为取消。 I'm new in php im not quiete understand it. 我是php的新手,我不安静地了解它。

The button containing text Accept is disabled - even if the value of $order_status is accepted , dispatched or cancelled . 即使$order_status的值被接受分派取消 ,包含文本Accept的按钮也被禁用。 Why is it always disabled? 为什么总是禁用它?

below is the code: 下面是代码:

<button type="submit" name="submitAccept" class="btn btn-primary" 
    <?php if ($order_status = 'Accepted'){?> disabled <?php } ?>">Accept </button> 

<button type="submit" name="submitSent" class="btn btn-primary" >Sent</button> 

<button type="submit" name="submitCancel" class="btn btn-danger" 
    onclick="if(!confirm('Are you sure you want to cancel order?')){return false;}">Cancel</button>

It's a good habit to separate your concerns. 分开担心是个好习惯。 First start with your HTML and as little PHP as possible: 首先从您的HTML和尽可能少的PHP开始:

<button
  type="submit"
  name="submitAccept"
  class="btn btn-primary"
  <?php if ($submitAcceptDisabled): ?> disabled <?php endif ?>">
  Accept
</button> 

The same goes for your other buttons, simply add a flag and keep your view simple. 其他按钮也是如此,只需添加一个标志并保持视图简单即可。 For your PHP logic let's investigate your scenarios: 对于您的PHP逻辑,让我们研究一下您的方案:

  1. When the order_status is Accepted only accept button is disabled. 当order_status被接受时,仅接受按钮被禁用。

     $submitAcceptDisabled = $order_status === 'Accepted'; 
  2. And If order_status is Dispatched the disabled button is sent. 并且,如果已调度order_status,则会发送已禁用按钮。

     $submitSentDisabled = $order_status === 'Dispatched'; 
  3. And if order_status is Cancelled the disabled button is Cancel. 如果order_status被取消,则禁用按钮为取消。

     $submitCancelDisabled = $order_status === 'Cancelled'; 

In your piece of code you assign a value to $order_status rather than checking if the current value equals something: 在您的代码段中,您将一个值分配给$order_status而不是检查当前值是否等于:

if ($order_status = 'Accepted')

This will always set the value of Accepted to the $order_status variable. 这将始终将Accepted的值设置为$order_status变量。

Another improvement you could consider is to separate your Javascript: 您可以考虑的另一个改进是分离Javascript:

const submitCancel = document.querySelector('button[name="submitCancel"]');
submitCancel.addEventListener('click', () => {
  if(!confirm('Are you sure you want to cancel order?')) {
    return false;
  }
});

The lone equal sign in your conditional statement is not a " Comparison Operator ", it is an " Assignment Operator ". 条件语句中的唯一等号不是“ 比较运算符 ”,而是“ 赋值运算符 ”。

If you write: 如果您写:

$order_status='Dispatched';
if($order_status='Accepted'){  // this sets the value
    echo "order_status=$order_status";  // this displays the new value
}else{
    echo "order_status=$order_status";  // this doesn't get a chance to happen
}

You will see: 你会看见:

order_status=Accepted

This is because the if statement is behaving like a declaration instead a comparison. 这是因为if语句的行为类似于声明,而不是比较。 To make your if statement compare the variable to the value, you can use == (loose comparison, aka true after "type juggling") or === (strict comparison, aka "identical"). 要使您的if语句将变量与值进行比较,可以使用== (松散比较,在“类型变戏法”之后也称为true)或=== (严格比较,又称为“相同”)。

For your task, you can use this snippet that employs inline-conditions in a single php code block. 对于您的任务,您可以使用在单个php代码块中采用内联条件的代码段。

Code: ( Demo ) 代码:( 演示

echo "<button type=\"submit\" name=\"submitAccept\" class=\"btn btn-primary\"",($order_status=='Accepted'?' disabled':''),">Accept<button>";
echo "\n";
echo "<button type=\"submit\" name=\"submitSend\" class=\"btn btn-primary\"",($order_status=='Dispatched'?' disabled':''),">Send<button>";
echo "\n";
echo "<button type=\"submit\" name=\"submitCancel\" class=\"btn btn-danger\"",($order_status=='Cancelled'?' disabled':'')," onclick=\"if(!confirm('Are you sure you want to cancel order?')){return false;}\">Cancel<button>";

Unrendered HTML Output: 未呈现的HTML输出:

<button type="submit" name="submitAccept" class="btn btn-primary" disabled>Accept<button>
<button type="submit" name="submitSend" class="btn btn-primary">Send<button>
<button type="submit" name="submitCancel" class="btn btn-danger" onclick="if(!confirm('Are you sure you want to cancel order?')){return false;}">Cancel<button>

A couple of extra notes: 一些额外的注意事项:

  • You had an extra " after ?> in your code here: ?>">Accept </button> 你有一个额外的"?>在你的代码在这里: ?>">Accept </button>
  • I change Sent to Send in my snippet for improved English 我更改了“ SentSend代码段以提高英语水平

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

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