单击复选框后显示/隐藏输入

时间:2016-02-14 13:19:29

标签: php jquery

我在检查/取消选中复选框后尝试显示/隐藏我的输入,但是我从DB获取此信息,所以,我正在使用PHP将'checked'属性添加到我的输入中。

jQuery代码正在运行,但是当我刷新页面时,即使选中“已启用”属性,我的输入也不会显示。

PHP代码

  <label>Send cash?</label>
  <input class="reg_cash" type="checkbox" name="reg_cash" value="1" 
  <?php echo ($configs->reg_cash) ? 'checked' : '' ?>>


  <div class="reg_cash_amount">     
    <label>Amount of cash</label>
       <input type="text" name="reg_cash_amount" id="coupon_field"/>
   </div>

Jsfiddle:https://jsfiddle.net/qmmg3qwo/

1 个答案:

答案 0 :(得分:1)

尝试根据数据库值隐藏/显示输入字段。

php code

<label>Send cash?</label>
  <input class="reg_cash" type="checkbox" name="reg_cash" value="1" 
  <?php echo ($configs->reg_cash) ? 'checked' : '' ?>>

<?php if($configs->reg_cash) { ?>
  <div class="reg_cash_amount">     
    <label>Amount of cash</label>
       <input type="text" name="reg_cash_amount" id="coupon_field"/>
   </div>
<?php } ?>

Jquery代码

$(".reg_cash").click(function () {
    if ($(this).is(":checked")) {
        $(".reg_cash_amount").show();
    } else {
        $(".reg_cash_amount").hide();
    }
});
相关问题