【发布时间】:2012-06-07 08:19:50
【问题描述】:
尝试了这个最新的一组代码的一些组合。简而言之,我将我的 javascript 从一个单独的文件运行到 html,我要做的就是设置一些代码来观察复选框何时更改为在其上运行一些逻辑。
尝试通过 .document.userdetails.signal、#signal(为此添加了类)和普通信号进行观看。
我希望得到一些指导,因为我在同一个 html 文件中查看了许多示例,这就是为什么我认为这是我尝试处理按钮的方式。
<form id="userdetails" name="userdetails" action="javascript: savesettings()" >
<p>
<p>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<input name="signal" type="checkbox" class="custom" id="signal" value="1" checked="true" />
<label class="signal" for="signal">Traffic Data</label>
</legend>
</div>
还有 Javascript
$(".document.userdetails.signal").click(function(e){
e.preventDefault();
alert('signal clicked!');
});
谢谢
人族
编辑 - 上次使用此示例 - Click event not firing in jQuery Mobile?
EDIT2 - http://forum.jquery.com/topic/calling-a-script-when-button-clicked 添加了 $(document).ready 所以现在 $(document).ready(".document.userdetails.signal").click(function(e){........ 似乎当我打开应用程序时注意到变化 - 我想我现在需要找到正确的目标,因为 Jquery 重新创建了复选框等。
EDIT 3 - Jasons 解决方案,尽管来自目标周围其他贡献者的 cmets 很有用。
$(function() {
$("#userdetails").on("change","#signal",function(eventObj) {
//do code here.
eventObj.preventDefault();
});
});
【问题讨论】: