Dan*_*Boi 2 wordpress checkbox jquery
我被困在如何检查是否使用 jQuery 选择了一个复选框。基本上,如果复选框被选中,那么我需要它来运行一个函数。
在 WordPress 帖子中,我只需要使用使用此 ID 的短代码:
[contact-form-7 id="78" title="BusinessForm"]
Run Code Online (Sandbox Code Playgroud)
然而,它以 HTML 的形式输出:
<span class="wpcf7-form-control-wrap sameAddress">
<span class="wpcf7-form-control wpcf7-checkbox sameAddress" id="sameAddress">
<span class="wpcf7-list-item first last">
<input type="checkbox" name="sameAddress[]" value="Yes">
<span class="wpcf7-list-item-label">Yes</span>
</span>
</span>
</span>
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的 jQuery:
$(input[value='sameAddress[]']).change(function() {
var alreadyChecked = $(this).hasClass('alreadyChecked');
if (alreadyChecked) {
alert('This does not have the class');
$(this).addClass('alreadyChecked');
} else {
alert('This has the class, yay!!!!! ');
}
});
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我改进它并使其在选中复选框时运行功能/警报吗?提前致谢。
编辑:这是一个小提琴:http : //jsfiddle.net/uk3E3/
你绑定事件的方式不对,像这样使用属性选择器,
$("input[name = 'sameAddress[]']").change(function () {
if (this.checked) {
alert("checked");
}
});
Run Code Online (Sandbox Code Playgroud)
在您的代码中,没有带有 value 的复选框sameAddress[]。因此它不会将更改事件绑定到任何复选框。
this.checked 如果复选框被选中,将返回 true。
| 归档时间: |
|
| 查看次数: |
7314 次 |
| 最近记录: |