[英]How can I set the value of an input the same as the value of the radio selected?
var value = $(".radioc").attr("value"); $("input[name=user-type]").change(function() { $(".input-txt-choose").val(value).toggle(this.value === value); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="radio" class="radioc" name="user-type" value="Brand">Brand <input type="radio" class="radioc" name="user-type" value="Store">Store <input type="text" class="input-txt-choose" value="">
在输入( .input-txt-choose
)中显示值( .radioc
),当我 select收音机( .radioc
)
当我 select 选项“存储”时,输入( .input-txt-choose
)隐藏。
这是我的小提琴
$("input[name=user-type]").change(function(){
$(".input-txt-choose").val($(this).val());
})
输入将消失,因为 function .toggle()
用于隐藏/显示 jquery 的元素。
由于实现了切换,您正在获得无线电隐藏,因此请使用以下代码,这将解决您的目的。
$(document).on("click", ".radioc", function () {
$(".input-txt-choose").val($('.radioc:checked').val());
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.