我需要为一组单选按钮注册一个处理程序。 我正在使用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>
is
是一个jQuery函数:http://docs.jquery.com/Is(尽管我没有在api.jquery.com上找到它,这让我有些担心)。 - Quintin Robinsonis()
函数 绝对 在 api 子域名下: api.jquery.com/is/。 - David Thomas