如何禁用if条件中的按钮

时间:2014-08-21 09:11:17

标签: php jquery html css button

如果prod_quantity == 0?

,如何禁用按钮?
<?php
    $prod_qty = $row['prod_quantity'];

            if ($prod_qty == '0'){
                echo "<h1>sold out</h1>";
            }
?>

这是我在prod_qty == 0

时需要禁用的按钮
<input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row["prod_id"]?>)" />

5 个答案:

答案 0 :(得分:17)

如果if如下所示,请在按钮中添加disabled条件,将属性设置为$prod_qty == '0'

<input type="button" value="Add to Cart" <?php if ($prod_qty == '0'){ ?> disabled <?php   } ?> onclick="addtocart(<?php echo $row["prod_id"]?>)" />

答案 1 :(得分:1)

if($prod_qty==0)
{
     <?php
     <input type="button" value="Add to Cart" disabled />
     <?php
}

更新2:

<input type="button" value="Add to Cart" 
  <?php if($row["prod_qty"]==0) 
  {
     echo ' onclick="addtocart('.$row["prod_id"].')" ';
  }
  else
  {
       echo ' disabled=disabled ';
  }
?>
 />

答案 2 :(得分:1)

<?php
$prod_qty = $row['prod_quantity'];

        if ($prod_qty == '0'){
            echo "<h1>sold out</h1>";
            echo '
                <input 
                    type="button" 
                    value="Add to Cart" 
                    onclick="addtocart('.$row['prod_id'].')" 
                    disabled
                />'
        }else{
            <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row["prod_id"]?>)" />
        }
?>

答案 3 :(得分:0)

试试这个

javascript onclick功能...

function addtocart(prod_qty){
    var quantity=parseInt(prod_qty);
    if (quantity == 0){
         document.getElementById("addtocartButton").disabled = true;   
    }

}

你的按钮

<input type="button" value="Add to Cart" id="addtocartButton" onclick="addtocart(<?php echo $row["prod_id"]?>)" />

答案 4 :(得分:0)

<?php
    $prod_qty = $row['prod_quantity'];

            if ($prod_qty == '0'){
                echo "<h1>sold out</h1>";
            }
?>

<input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row["prod_id"]?>)" {{($prod_quantity == 0) ? 'disabled' : ''}} />