Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery自定义复选框-图形更新不起作用_Jquery_Checkbox - Fatal编程技术网

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");