简体   繁体   English

复选框创建在ie7中不起作用

[英]Check box creation doesn't work in ie7

$("div").append($(document.createElement("input")).attr({
                         'id':    'post_category',
                         'value': 'item',
                        'type':  'checkbox','checked': true
                     }));

I am trying to create checkbox with checked but ie7 display nothing. 我试图创建复选框已选中但ie7显示任何内容。 while other browsers displays checkbox as checked. 而其他浏览器显示复选框已选中。 How to display checkbox selected in ie7 如何显示在ie7中选中的复选框

Any reason you can't do it like this: 你不能这样做的任何理由:

$("div").append("<input id='post_category' value='item' type='checkbox' checked='checked'>");

You could construct it dynamically, as per your original intention: 您可以根据您的初衷动态构建它:

$("div").append($("<input>").attr({
                     'id':    'post_category',
                     'value': 'item',
                    'type':  'checkbox','checked': true
                 }));

As per this JSFiddle: http://jsfiddle.net/ReErJ/2/ 按照这个JSFiddle: http//jsfiddle.net/ReErJ/2/

I also notice that in your code you have 我也注意到你的代码中有

'checked': true

That should be 那应该是

'checked': 'checked'

尝试设置'checked':'checked' ,而不是true

Code works in other browser 代码适用于其他浏览器

$("div").append("<input id='post_category' value='item' type='checkbox' checked='checked'>");

Solution To insert checkbox as checked in ie7 解决方案在ie7中插入复选框

var category_checkbox = $("<input>").attr({'name' : 'post_category[]' , 'id' : 'post_category' , 'type' : 'checkbox' , 'value' : '1'});

$("div").append(category_checkbox);

category_checkbox.attr({'checked' : 'checked'})

IE7 wont accept checked attribute while inserting elements. IE7在插入元素时不会接受checked属性。 so once we append checkbox refer that checkbox and set attribute as checked. 所以一旦我们追加复选框,请参阅该复选框并将属性设置为已选中。

I copied the code you posted into a new page, and I get 2 checkboxes in IE7. 我将您发布的代码复制到新页面,我在IE7中获得了2个复选框。 Perhaps you have an issue somewhere else in your code? 也许你的代码中的其他地方有问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM