[英]Populate textbox based on selected radio button
我有3個單選按鈕,當選擇其中一個時,我想自動將文本放在文本框中。 例如,如果用戶選擇了“ TBA”單選按鈕,則我希望在文本框中輸入“待建議”文本,如果他們選擇了另一個單選按鈕,則我希望將其清除。
誰能幫我??
提前致謝!
應該這樣做:
$('input[name="dest_type"]').on('change', function() {
$('input[type="text"]').val($(this).val());
});
現場演示: http : //jsfiddle.net/ej2hf/2/
然后試試這個
http://jsfiddle.net/ipsjolly/KNct8/
$('input[name="dest_type"]').on('change', function() {
if($(this).val() == "tba"){
$('input[type="text"]').val($(this).val());
}
else
{
$('input[type="text"]').val('');
}
});
要么
http://jsfiddle.net/ipsjolly/KNct8/1/
$('input[name="dest_type"]').on('change', function() {
if($(this).val() == "tba"){
$('input[type="text"]').val('To Be Advised');
}
else
{
$('input[type="text"]').val('');
}
});
除了使用在頁面加載時默認選擇的值填充文本框之外,請使用以下代碼:
$(document).ready(function(){
$('input[type="text"]').val($('input[name="dest_type"]').val());
$('input[name="dest_type"]').on('change', function() {
$('input[type="text"]').val ($(this).val()); });
});
假設您已經或可以使用最新的jQuery ,則可以使用以下代碼:
$(function() {
$('input[name="dest_type"]').on("change", function() {
$('input[type="text"]').val($(this).val());
});
});
見小提琴!
如果需要切換TBA的值,請使用以下命令:
$(function() {
$('input[name="dest_type"]').on("change", function() {
$('input[type="text"]').val( (($(this).val()=='tba')?($(this).val()):('')) );
});
});
看到這個小提琴!
如果這不是您要嘗試執行的操作,請重新說明問題或添加更多信息!
嘗試這個,
$(document).ready(function () {
$('input:radio[name="dest_type"]').change(function () {
$("input:text").val($(this).val());
});
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.