【发布时间】:2017-03-13 17:45:06
【问题描述】:
在表单中,我有一组带有固定捐赠金额的单选按钮和另一个字段,当用户可以输入选择的金额时。为简单起见,在处理表单时,我想要一个功能,其中选择的任何内容都填充在另一个字段中。我有下面的 HTML/JS:
$("input[id^='radio']").click(function() {
$("input[name='otheramount']").val(jQuery(this).val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="radio-select">
<input type="radio" name="selamount" id="amount-1" value="10.00">
<label for="amount-1">$10</label>
</div>
<div class="radio-select">
<input type="radio" name="selamount" id="amount-2" value="15.00">
<label for="amount-2">$15</label>
</div>
<div class="radio-select">
<input type="radio" name="selamount" id="amount-3" value="20.00">
<label for="amount-3">$20</label>
</div>
<div class="radio-select">
<input type="radio" name="selamount" id="amount-3" value="20.00">
<label for="amount-4">$25</label>
</div>
<div class="form-group other-amount col-lg-3 col-md-8 col-xs-12 padd-top-10"></div>
<input type="text" id="otheramount" name="otheramount" value="" placeholder="Or Other Amount">
我也试过
<input type="radio" name="selamount" id="amount-3" value="20.00" onclick="document.getElementById('otheramount').value = this.value">
但是当第一个改变时,它在随后的点击中不再改变。
【问题讨论】:
-
没有
<input>元素的 id 属性包含单词radio。
标签: javascript jquery radio-group