选择单选按钮时更改隐藏的输入字段值 [英] Change a hidden input fields value when a radio button is selected

查看:101
本文介绍了选择单选按钮时更改隐藏的输入字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每次用户单击单选按钮时都更改隐藏输入字段的值.

I want to change the value of an hidden input field each time a user checks a radio button.

这是我的HTML外观.

This is how my HTML looks like.

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

有人可以帮忙吗?

亲切的问候,

Steaphann

Steaphann

推荐答案

您的内联onclick处理程序将不起作用,因为您没有包含括号:实际调用函数必须为onclick="change()".但是您应该将其更改为onclick="change(this);",以将对单击按钮的引用传递给函数,然后:

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:

function change(el){
    $("#ZAAL_NAME").val(el.value) ;
}

此外,您的jQuery选择器还需要一个#来按ID进行选择.

Also you jQuery selector need a # to select by id.

更好的方法是从标记中删除内联onclick并执行以下操作:

Better though would be to remove the inline onclick from your markup and do it like this:

$('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.

注意:在获取clicked元素的值时,您可以说$(el).val()$(this).val()(取决于您正在查看的两个函数中的哪一个),但是假定elthis是对DOM元素,您可以直接使用el.valuethis.value获取其value属性.

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.

这篇关于选择单选按钮时更改隐藏的输入字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆