Value of jQuery generated checkbox in IE8 is stored as “on” rather than actual value?

。_饼干妹妹 提交于 2019-12-02 02:04:09
GodsBest

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

I found a solution for anyone whom is interested. It's more of a work around as I couldn't figure out why it was happening.

On creating each input element instead of popuating the value field which just gets set to 'on' in IE when using jquery 1.4. I created a val attr for each element and store the category id. Then I simply call this code on submit to harvest the results.

$(document).ready(function() {
        $("form").submit(function(){

            var str = "";
                $("#ProfSelector input:checkbox:checked").each(function () {
                    str += $(this).attr("val") + ",";
                });

            $("form").append( $("<input></input>").attr({ "type":"text", "name":"selectedCategories", "value":str }));
        });
    });

I find the .map function to be useful for this scenario. No need to strip off a trailing ',' with this usage.

$('#ProfSelector input:checkbox:checked').map(function() {
  return this.id;
  // or jquery object 
  // return $(this).val();
}).get().join(',');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!