IE8中生成的jQuery值的复选框存储为“on”。而不是实际价值? [英] Value of jQuery generated checkbox in IE8 is stored as "on" rather than actual value?

查看:152
本文介绍了IE8中生成的jQuery值的复选框存储为“on”。而不是实际价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例代码适用于FireFox,但IE导致问题。

The following example code works in FireFox but IE is causing problems.

此代码实质上根据JSON数组呈现动态复选框列表。

This code essentially renders a list of dynamic checkboxes according to a JSON array.

当我尝试提交variblse时,复选框的值存储为on。我注意到有一个额外的属性被渲染(仅限IE),名为jQuery1288631121994,存储真实值。好像jquery试图管理复选框的状态,但我似乎无法访问存储的值?

When I try and submit the variblse the value for the checkboxes are stored as "on". I've noticed there is an additional attribute that gets rendered (IE only) called jQuery1288631121994 which stores the real value. It seems like jquery is trying to manage the state of checkboxes but I cant seem to access the stored values?

这是我的测试示例:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="js/jquery-1.4.3.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">


        var state = {
            Professions: [1]
        };

        $(document).ready(function () {



            var data = [{ "ID": 1, "Name": "Football" }, { "ID": 2, "Name": "Cricket" }, { "ID": 3, "Name": "Hockey"}];

            $.each(data, function () {

                var catid = this['ID'];
                var catname = this['Name'];

                var selected = $.inArray(catid, state.Professions) != -1 ? true : false;

                $("<li></li>")
                    .append(
                        $("<input></input>").attr({
                            id: 'category' + catid
                            , name: 'categories'
                            , value: catid
                            , type: 'checkbox'
                            , checked: selected
                        })
                        .click(function (event) {
                            //alert($(this)[0].value);

                        })
                    )
                    .append(
                            $(document.createElement('label')).attr({
                                'for': 'category-' + catid
                            })
                            .text(catname)
                    )
                    .append(
                        $("<div></div>").addClass("clear")
                    )
                    .appendTo("#ProfSelector ul");

            });

                        $("#btnTest").click(function () {
                alert($("#ProfSelector input:checkbox:checked").val());
            });

        });
    </script>
</head>
<body>
    <div id="ProfSelector">
        <ul>
        </ul>
    </div>
    <a href="#" id="btnTest">Test</a>
</body>
</html>


推荐答案

看起来IE似乎不喜欢复选框的方式属性设置;如果更改为以下IE很高兴: http://jsfiddle.net/tS3cs/

It would seem IE does not like the way the checkbox attributes are set; if that is changed to the following IE is happy: http://jsfiddle.net/tS3cs/

$("<li></li>")
.append(
    '<input id="category'+catid+'" value="'+catid+'" type="checkbox"></input>'
)
.append(
    $(document.createElement('label')).attr({
        'for': 'category-' + catid
    })
    .text(catname)
)
.append(
    $("<div></div>").addClass("clear")
)
.appendTo("#ProfSelector ul");

$('#category'+catid).attr('checked',selected);

这篇关于IE8中生成的jQuery值的复选框存储为“on”。而不是实际价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆