【问题标题】:How can I hide element using jQuery? [duplicate]如何使用 jQuery 隐藏元素? [复制]
【发布时间】:2016-12-13 18:07:36
【问题描述】:

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");
});

我正在尝试在选择第二个无线电选项时显示TextBox ID = TRX_ID,但是当取消选择时,我希望要隐藏的文本框。我该怎么做?

【问题讨论】:

  • 您的代码已经在这样做了。不是吗?
  • 不。但 Roko 的回答对我有用。

标签: javascript jquery html hide


【解决方案1】:
  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"/>

【讨论】:

  • 谢谢伙计。解决了我的问题。
  • @MiniGunnR 不客气;)
猜你喜欢
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
相关资源
最近更新 更多