如何使用jQuery隐藏元素



HTML

<div class="btn-group" data-toggle="buttons">
            <label class="btn btn-default active">
                <input type="radio" name="payment_options" value=1 autocomplete="off" checked>Cash on Delivery
            </label>
            <label class="btn btn-default" id="bkash-radio">
                <input type="radio" name="payment_options" value=2 autocomplete="off">bKash
            </label>
        </div>
        <input type="hidden" name="trx_id" id="trx_id" class="form-control" placeholder="Enter Transaction ID"/>

jQuery函数:

$(document).on("click", "#bkash-radio", function() {
    console.log('test');
    $("#trx_id").attr("type", "text");
});
$(document).on("blur", "#bkash-radio", function() {
    console.log('test');
    $("#trx_id").attr("type", "hidden");
});

当选择第二个单选选项时,我试图显示文本框id=trx_id,但当取消选择时,我希望隐藏文本框。我该怎么做?

  1. 瞄准您的[name='payment_options']收音机
  2. change事件上查看this.value==="2"
  3. 然后分别操作prop类型

$(document).on("change", "[name='payment_options']", function() {
  $("#trx_id").prop("type", this.value==="2" ? "text" : "hidden");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-default active">
    <input type="radio" name="payment_options" value=1 autocomplete="off" checked>Cash on Delivery
  </label>
  <label class="btn btn-default" id="bkash-radio">
    <input type="radio" name="payment_options" value=2 autocomplete="off">bKash
  </label>
</div>
<input type="hidden" name="trx_id" id="trx_id" class="form-control" placeholder="Enter Transaction ID"/>

相关内容

  • 没有找到相关文章

最新更新