0

i used below code for custom checkbox,

HTML

<div class="agree">
                                      <label for="agree_check" class="label_check"><input type="checkbox" class="agree_check" id="agree_check" />Agree</label>
                                  </div>

CSS

.has-js .label_check { padding-left: 34px; }
.has-js .label_check { background: url(../images/box1.png) no-repeat; }
.has-js label.c_on { background: url(../images/box1-with-mark.png) no-repeat; }
.has-js .label_check input { position: absolute; left: -9999px; }

Script

<script type="text/javascript">
var d = document;
var safari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1) ? true : false;
var gebtn = function(parEl,child) { return parEl.getElementsByTagName(child); };
onload = function() {

    var body = gebtn(d,'body')[0];
    body.className = body.className && body.className != '' ? body.className + ' has-js' : 'has-js';

    if (!d.getElementById || !d.createTextNode) return;
    var ls = gebtn(d,'label');
    for (var i = 0; i < ls.length; i++) {
        var l = ls[i];
        if (l.className.indexOf('label_') == -1) continue;
        var inp = gebtn(l,'input')[0];
        if (l.className == 'label_check') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_check c_on' : 'label_check c_off';
            l.onclick = check_it;
        };
        if (l.className == 'label_radio') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_radio r_on' : 'label_radio r_off';
            l.onclick = turn_radio;
        };
    };
};
var check_it = function() {
    var inp = gebtn(this,'input')[0];
    if (this.className == 'label_check c_off' || (!safari && inp.checked)) {
        this.className = 'label_check c_on';
        if (safari) inp.click();
    } else {
        this.className = 'label_check c_off';
        if (safari) inp.click();
    };
};
var turn_radio = function() {
    var inp = gebtn(this,'input')[0];
    if (this.className == 'label_radio r_off' || inp.checked) {
        var ls = gebtn(this.parentNode,'label');
        for (var i = 0; i < ls.length; i++) {
            var l = ls[i];
            if (l.className.indexOf('label_radio') == -1)  continue;
            l.className = 'label_radio r_off';
        };
        this.className = 'label_radio r_on';
        if (safari) inp.click();
    } else {
        this.className = 'label_radio r_off';
        if (safari) inp.click();
    };
};
       </script>

now i need to check the checkbox is checked or not.. it is works in FF but not works in Chrome and Safari browser.

if (jQuery('#agree_check').is(':checked')) {
alert("checked");
}
else
{
alert("Not checked");
}

when i checked the checkbox it displays "checked" in FF.. but in Chrome and safari it displays "not checked".

what is the issue?

4

2 回答 2

1

我的解决方案也在服务器端工作 - 代码隐藏(标签和复选框的 runat="server" 属性)包括为关键浏览器添加选中/未选中状态。也许这适用于所有浏览器,清除if声明。

 var check_it = function () {
     var inp = gebtn(this, 'input')[0];
     if (this.className == 'label_check c_off' || (!safari && inp.checked)) {
         this.className = 'label_check c_on';
         if (chrome) inp.checked = true;
         if (safari) inp.click();
     } else {
         this.className = 'label_check c_off';
         if (chrome) inp.checked = false;
         if (safari) inp.click();
     }; 
};
于 2012-11-01T07:52:43.280 回答
0

最后我找到了答案,它只是以另一种方式检查以使该条件为真,

if (jQuery('#agree_check').is(':checked') || (jQuery('div.agree label').hasClass('c_on'))) {
alert("checked");
}
else
{
alert("Not checked");
}

现在它在所有浏览器中都可以正常工作。

谢谢, 拉维钱德兰

于 2012-06-16T06:44:07.550 回答