如何使用 jQuery 隐藏元素?

How can I hide element using 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 type 分别

$(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"/>