javascript 选择单选按钮时更改隐藏的输入字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10849759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change a hidden input fields value when a radio button is selected
提问by Steaphann
I want to change the value of an hidden input field each time a user checks a radio button.
每次用户检查单选按钮时,我都想更改隐藏输入字段的值。
This is how my HTML looks like.
这就是我的 HTML 的样子。
<input type="hidden" id="ZAAL_NAME" value="" name="ZAAL_NAME" />
<input type="radio" name="zaal_ID" value="ROOM1" onclick="change">
<input type="radio" name="zaal_ID" value="ROOM2" onclick="change">
<input type="radio" name="zaal_ID" value="ROOM3" onclick="change">
Now I want that each time I check a radio button, the value of that radio button becomes also the value of the hidden input field. Does anybody knows how to do that? Here is what I have so far.
现在我希望每次检查单选按钮时,该单选按钮的值也成为隐藏输入字段的值。有人知道该怎么做吗?这是我到目前为止所拥有的。
$('input[name="radbut"]').click(function() {
console.log(this.value);
$('ZAAL_NAME').val(this.value);
});
Could anybody help ?
有人可以帮忙吗?
Kind regards,
亲切的问候,
Steaphann
斯蒂芬
回答by nnnnnn
Your inline onclick
handler won't work since you didn't include parentheses: it needs to be onclick="change()"
to actually call your function. But you should change it to onclick="change(this);"
to pass a reference to the clicked button to your function, then:
您的内联onclick
处理程序将不起作用,因为您没有包含括号:它需要onclick="change()"
实际调用您的函数。但是您应该将其更改onclick="change(this);"
为将点击按钮的引用传递给您的函数,然后:
function change(el){
$("#ZAAL_NAME").val(el.value) ;
}
Also you jQuery selector need a # to select by id.
此外,您的 jQuery 选择器需要一个 # 来按 id 进行选择。
Better though would be to remove the inline onclick
from your markup and do it like this:
更好的是onclick
从标记中删除内联并这样做:
$('input[name="zaal_ID"]').click(function() {
$('#ZAAL_NAME').val(this.value);
});
The latter would need to be in a document ready handler and/or in a script block that appears after your radio buttons in the page source.
后者需要在文档就绪处理程序和/或出现在页面源中的单选按钮之后的脚本块中。
Note: when getting the value of the clicked element you can say $(el).val()
or $(this).val()
(depending on which of the two functions you are looking at), but given that el
and this
are references to the DOM element you can just get its value property directly with el.value
or this.value
.
注意:当获取被点击元素的值时,您可以说$(el).val()
or $(this).val()
(取决于您正在查看的两个函数中的哪一个),但鉴于el
andthis
是对 DOM 元素的引用,您可以直接使用el.value
or获取其 value 属性this.value
。
回答by undefined
$('input[name="zaal_ID"]').change(function(){
$('#ZAAL_NAME').val( this.value );
})
回答by Dipak
try -
尝试 -
$(':radio').change(function(){
$('#ZAAL_NAME').val($(this).val());
})
回答by Maneesh mehta
use onclick="change(this.value);"
and the function use like this
使用onclick="change(this.value);"
和功能使用这样
function change(e){
$("ZAAL_NAME").val(e) ;
}
回答by Pethical
function change(){
$("ZAAL_NAME").val( $(this).val() ) ;
}
or
或者
function change(){
$("ZAAL_NAME").val($("input[type='radio']:checked").val()) ;
}
回答by Pranay Rana
You can use is
你可以使用是
$("input:radio[name=ZAAL_NAME]").click(function() {
var value = $(this).val();
// or $('input:radio[name=theme]:checked').val();
$("#ZAAL_NAME").val(value);
});
in short
简而言之
$("input:radio[name=ZAAL_NAME]").click(function() {
$("#ZAAL_NAME").val( $(this).val() );
});