在我的页面上更改复选框后运行函数
Run function after a checkbox change on my page
我在页面上动态创建了复选框,并为每个复选框分配了一个唯一的id,如"renametam1'"、"renametem2'"等。当其中一个复选框被选中时,我正试图运行一个函数。然后,该函数将允许用户输入以前只读的相应字段。
我试过以下方法,但似乎不起作用。
var a=0;
$('input[type=checkbox]').change(function () {
for (var i=0;i<rows3;i++){
a=a+1;
var id= '#renameteam'+a;
if ($(id).is(":checked")) {
$('#newname'+a).removeProp('readonly');
}
}
//Here do the stuff you want to do when 'unchecked'
});
有什么建议吗?
就是这样做的
//delegate the event so you can call this on document ready and it will still be bound to any dynamically created elements
$(document).on('change', 'input[type=checkbox]', function() {
var checkbox = $(this),
otherInput = $('#' + checkbox.data('id'));
otherInput.prop('readonly', !checkbox.is(':checked'));
if (!checkbox.is(':checked')) {
// do stuff here when checkbox isn't checked - not sure if you still want this bit but it is as per your comments in the code above
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="checkbox" value="" id="renameteam1" data-id="newname1" />
<!-- use a data attribute to target the id of the input you want to make readonly -->
<input type="textbox" name="textbox" value="" id="newname1" readonly />
如果你不想使用数据属性,你可以这样做:
//delegate the event so you can call this on document ready and it will still be bound to any dynamically created elements
$(document).on('change', 'input[type=checkbox]', function() {
var checkbox = $(this),
otherInput = $('#newname' + this.id.replace('renameteam', ''));
otherInput.prop('readonly', !checkbox.is(':checked'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="checkbox" value="" id="renameteam1" />
<!-- use a data attribute to target the id of the input you want to make readonly -->
<input type="textbox" name="textbox" value="" id="newname1" readonly />
尝试对复选框和单选按钮使用点击而不是更改。
相关文章:
- 嗨,我正试图将我的movieprice数组从复选框值传递给另一个计算选择总数的函数,但我的代码没有;t运行
- 为预先选中的复选框在加载时运行ng更改函数
- C#/ASP-web,ASP:在客户端执行复选框消息框,如果是,则运行服务器端代码
- 根据复选框运行函数
- 我的 javascript 语句不会运行其 else 语句,即使未选中复选框
- 在运行时添加复选框并将其保存在 HTML 文件代码中
- 如果选中复选框,则在登录后运行 javascript 函数
- 在单击禁用复选框时运行函数
- 在更改的众多复选框之一上运行javascript函数
- 动态复选框创建以在单击时运行函数
- 勾选复选框并通过单击链接运行 onclick 事件
- 如果选中了特定的复选框,我想运行另一段 jQuery 代码(如果是)
- 在实际设置checked标志之前,运行脚本onclick的复选框
- 将父样式更改应用于组中的所有复选框;全部检查”;运行javascript函数
- 路由加载时运行成员控制器函数以设置控制复选框的属性
- 在我的页面上更改复选框后运行函数
- 单击自定义复选框不会运行关联的onClick功能
- 如何在JavaScript/jQuery中弹出模态并运行复选框检查
- 复选框-仅在选中jquery时运行
- 如何让Dojo复选框运行命令