应用程序包括Jquery 1.9.1,unexcore.js和jquery plagin dataTables。
我的复选框包含如下:
<label style="top: 3px; position: relative;" class="checkbox" id="IsSpreadCheckBox">
<input type="checkbox" name="IsSpread" id="IsSpread">
<span></span>
</label>
元素 label.checkbox span 包含每种情况的背景图片:已选中,未选中,已停用。
Jquery onclick处理程序:
$(".checkbox span").click(function () {
var backgroundContainer = $(this); //span with background image
var checkbox = backgroundContainer.parent().find("input[type='checkbox']"); //hidden checkbox itself
if (!backgroundContainer.hasClass("disabled")) {
if (backgroundContainer.hasClass("checked")) {
checkbox.removeAttr('checked').prop("checked", false);
//Also tried checkbox.removeAttr('checked') or checkbox.prop("checked", false);
backgroundContainer.removeClass("checked");
}
else {
checkbox.attr('checked', 'checked').prop("checked", true);
//checkbox.attr('checked', 'checked') or checkbox.prop("checked", true);
backgroundContainer.addClass("checked");
}
}
});
此外,如果需要,我还有一些代码可以在页面加载后禁用此元素。
正确调试或隐藏复选框元素时正确选中或取消选中(复选框图像也正确)。此外,在目标浏览器 IE 10 (服务器端收到正确的复选框值)中工作正常,但在调试时使用 FireBug 在我的 FireFox 29 中在表单提交我看到:
$("#IsSpread").attr("checked") // checked
$("#IsSpread").prop("checked") // false
,服务器始终收到 false 。
调试此元素的属性更改不会产生任何效果。
如果我以编程方式检查 $(document).ready(),则会始终发送 true 条件。
谢谢!
答案 0 :(得分:0)
Fiddle使用.attr()
:
if (backgroundContainer.hasClass("checked"))
{
checkbox.removeAttr('checked');
}
else
{
checkbox.attr('checked', 'checked');
}
以后:
$('#send').click(function()
{
var checked = $('#IsSpread').attr("checked") == "checked";
alert("Sending " + checked);
});
Fiddle使用.prop()
(实际上这是一种奇怪的行为,但对我有用):
checkbox.prop('checked', checkbox.prop('checked'));
以后:
$('#send').click(function()
{
var checked = $('#IsSpread').prop("checked");
alert("Sending " + checked);
});