如何使用 JQuery 监听 RadioGroup 选定值的更改?
我需要为一组单选按钮注册一个处理程序。我正在使用 JQuery 并希望它的 .change 方法能够完成此任务。但是我没有经历过期望的行为。
这是我编写的示例片段。遗憾的是,“radioValueChanged”仅在初始加载时被调用。选择 true / false 不会触发处理程序。
<html>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<form id="myForm">
<div id="Question1Wrapper">
<div>
<input type="radio" name="controlQuestion" id="valueFalse" value="0" />
<label for="valueFalse">
False</label>
</div>
<div>
<input type="radio" name="controlQuestion" id="valueTrue" value="1" />
<label for="valueTrue">
True</label>
</div>
</div>
<div id="Question2Wrapper">
<div>
<label for="optionalTextBox">
This is only visible when the above is true</label>
<input type="text" name="optionalTextBox" id="optionalTextBox" value="" />
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function ()
{
$("#controlQuestion").change(radioValueChanged('controlQuestion'));
})
function radioValueChanged(radioName)
{
radioValue = $('input[name=' + radioName + ']:checked', '#myForm').val();
alert(radioValue);
if(radioValue == 'undefined' || radioValue == "0")
{
$('#Question2Wrapper:visible').hide();
}
else
{
$('#Question2Wrapper:visible').show();
}
}
</script>
</form>
I need to register a handler for a group of radio buttons. I'm using JQuery and hoped that its .change method would accomplish this. However I have not experienced the desired behavoir.
Here is a sample snippet I've written. Sadly, the "radioValueChanged" is only called on the initial load. Selecting either true / false does not trigger the handler.
<html>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<form id="myForm">
<div id="Question1Wrapper">
<div>
<input type="radio" name="controlQuestion" id="valueFalse" value="0" />
<label for="valueFalse">
False</label>
</div>
<div>
<input type="radio" name="controlQuestion" id="valueTrue" value="1" />
<label for="valueTrue">
True</label>
</div>
</div>
<div id="Question2Wrapper">
<div>
<label for="optionalTextBox">
This is only visible when the above is true</label>
<input type="text" name="optionalTextBox" id="optionalTextBox" value="" />
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function ()
{
$("#controlQuestion").change(radioValueChanged('controlQuestion'));
})
function radioValueChanged(radioName)
{
radioValue = $('input[name=' + radioName + ']:checked', '#myForm').val();
alert(radioValue);
if(radioValue == 'undefined' || radioValue == "0")
{
$('#Question2Wrapper:visible').hide();
}
else
{
$('#Question2Wrapper:visible').show();
}
}
</script>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有几个问题。
您将在脚本执行后立即运行
radioValueChanged('controlQuestion')
,因为这是方法调用而不是函数赋值。选择器
$("#controlQuestion")
错误,您没有任何 id 为controlQuestion
的元素。radioValueChanged
方法未正确处理值,因为它们将传递给 jQuery 事件处理程序。您可以尝试如下操作:
老实说,我不确定这是否是您在 if 语句中寻找的实际逻辑,但希望这将为您提供纠正当前代码的基础。
There are a few issues here.
You are immediately running
radioValueChanged('controlQuestion')
upon script execution because that is a method call and not a function assignment.The selector
$("#controlQuestion")
is wrong, you don't have any elements with id ofcontrolQuestion
.The
radioValueChanged
method is not properly handling values as they would be passed to a jQuery event handler.You could try something like the following:
In all honesty I'm not sure if that is the actual logic you are looking for with the if statement, but hopefully this will provide a basis for you to correct the current code.
取出 :visible,只有当 div 已经显示时才会选择它,实际上,如果它被隐藏,它就永远不会显示。
我离题了,我认为昆廷说中了大部分观点。这里有一些问题。
Take out the :visible, this will only select it if the div is already showing, in effect it will never be shown if it is hidden.
I digress, I think Quintin hits most of the points. There are a few issues going on here.