javascript - 使用表单向导检测是否单击了复选框
问题描述
我目前正在使用表单向导:
<form name="example-1" id="wrapped" action="" method="POST">
<div id="middle-wizard">
<div class="step">
</div>
<div class="step">
</div>
<div class="step">
<div class="row">
<div class="col-md-6">
<ul class="data-list-2 clearfix">
<li><input name="information[]" id ="other_info" class="otherInfo check_radio" type="checkbox" value="Others"><label>Others</label></li>
</ul>
</div>
</div>
</div>
</div>
<div id="bottom-wizard">
<button type="button" name="backward" class="backward"> Backward </button>
<button type="button" name="forward" class="forward"> Forward </button>
</div>
</form>
我正在尝试检查复选框是否被选中。我做的第一件事是控制台记录复选框的 id。
控制台返回复选框,但是当我尝试在复选框 id 上创建单击函数时,单击函数不会引发控制台日志。
这是我尝试过的点击功能示例:
console.log('#other_info')
$("#other_info").click(function(){
if(!$(this).is(':checked'))
{
console.log('yes its checked')
}
else
{
console.log('no its not!!')
}
})
此表单使用 iCheck 自定义插件进行复选框,因此无法正常单击或更改功能。通过使用 ichecked 提供的自定义函数完成了问题的修复
解决方案
click
在这种情况下,您不应使用侦听器,而应使用change
侦听器。我相信它的使用效率要高得多。
这是一个代码片段作为快速外卖(工作小提琴):
document.querySelector('#wrapped').addEventListener('change', function(event) {
const allClicked = this.querySelectorAll('input[type="checkbox"]:checked')
console.log(allClicked)
console.log(`Chief, I am clicked: ${event.target}!`) // Show what is currently being clicked
})
您也可以在我的类似回复中阅读有关该主题的更多信息: 如何计算每个选中的复选框
这是一个更复杂的小提琴:https ://jsfiddle.net/mkbctrlll/yak4oqj9/159/
在里面你有 3 种可能的方法来解决你的问题。希望这可以帮助
推荐阅读
- python - 在Python中将元组列表与总和值合并
- java - MediaStore 光标未在 Android 10 中显示非媒体文件
- python-3.x - 发送到网络的 Python 数据
- java - SwaggerUi 不从 yml 文件中取数据
- json - 从 Google Places Review 添加评论者图片
- docker - Docker 不会杀死容器
- sql-server - 安装 Informatica 10.4.1 时连接数据库时出现数据库连接错误 l
- .net-core - 如何使用 .net core 3.1 后端设置分布式系统
- android - Cordova 无法为 android 构建 APK
- android - 移动应用链接在不同渠道中的处理方式不同