jQuery自定义复选框-图形更新不起作用
尝试在窗体加载时设置自定义复选框值。调试告诉我复选框反映从源加载的值,但自定义图形不会更新 以我的形式,我有jQuery自定义复选框-图形更新不起作用,jquery,checkbox,Jquery,Checkbox,尝试在窗体加载时设置自定义复选框值。调试告诉我复选框反映从源加载的值,但自定义图形不会更新 以我的形式,我有 <input class="checkbox" type="checkbox" name="uitem_autorenew" id="uitem_autorenew" /> 代码包括以下内容 $(document).ready(function() { $('.checkbox').after(function() {
<input class="checkbox" type="checkbox" name="uitem_autorenew" id="uitem_autorenew" />
代码包括以下内容
$(document).ready(function() {
$('.checkbox').after(function() {
if ($(this).is(":checked")) {
return "<a href='#' class='toggle checked' ref='" + $(this).attr("id") + "'></a>";
} else {
return "<a href='#' class='toggle' ref='" + $(this).attr("id") + "'></a>";
}
});
我只是想知道在风格没有改变的情况下我缺少了什么。
欢迎提供任何线索,并提前表示感谢。
Kevin您可以在checkbox元素上切换“checked”类,但您希望为动态创建的a-tag更改它。所以试着改变
$("#uitem_autorenew").toggleClass("checked");
到
var ar=$(this).data('renew');
console.log("Value of ar = " + ar); // which is Y
if (ar =="Y"){
$("#uitem_autorenew").prop('checked',true); // tried this
$("#uitem_autorenew").toggleClass("checked"); //tried this too
}
console.log ("Is checkbox checked?");
console.log ($("#uitem_autorenew").prop('checked')); // yes it is but the graphic has not toggled. Top left of toggle.png is has word "YES"
$("#uitem_autorenew").toggleClass("checked");
$(".toggle").toggleClass("checked");