我想在单击单选按钮时在其上应用一个事件...
我尝试使用以下代码:my code
<div id="inline_content">
<form class="type" >
<header class="ui-header">
<h2 class="ui-title">Duration</h2>
</header>
<div class="ui-content">
<ul class="ui-listview">
<li class="li-has-radio">
<label>
15 mn
<input type="radio" value="15" name="radSize" checked="checked"/>
</label>
</li>
<li class="li-has-radio">
<label>
30 mn
<input type="radio" value="30" name="radSize"/>
</label>
</li>
<li class="li-has-radio">
<label>
45 mn
<input type="radio" value="45" name="radSize"/>
</label>
</li>
</ul>
</div>
</form>
</div>
还有 JavaScript:
$("#inline_content input[name='type']").click(function(){
alert('You clicked radio!');
// if($('input:radio[name=type]:checked').val() == "walk_in"){
alert($('input:radio[name=type]:checked').val());
//$('#select-table > .roomNumber').attr('enabled',false);
//}
});
但这似乎不起作用!
提前致谢。
最佳答案
一些问题:
- 您没有在 fiddle 中包含 jQuery,因此
$
未定义。 - 您没有任何名为
type
的输入
- 您应该监听
change
事件,而不是click
事件,因为输入可以通过其他方式更改,例如使用键盘。
这有效(如果你有 jQuery):
$("#inline_content input[name='radSize']").on('change', function(){
alert('You clicked radio!');
});
关于javascript - 在单选按钮上添加事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24643220/