简体   繁体   English

jQuery复选框样式问题

[英]jQuery checkbox styling issue

I have a piece of jQuery code, which I use to check if a checkbox is not checked. 我有一段jQuery代码,我用它来检查是否未选中复选框。 If not, it throws an event. 如果没有,它会抛出一个事件。 Here it is: 这里是:


$('#Button').click(function() {
        if(!$('input[@name=\"image_id\"]:checked').val()){
            //Do Something
            return false;
            }
});

It works great, but i decided to style the default checkbox with jQuery Checkbox plugin. 它工作得很好,但我决定使用jQuery Checkbox插件设置默认复选框的样式。

Now heres the problem, This is what the output is after styling: 现在是问题所在,这就是造型后的输出:

<input type="radio" name="answer_id" value="37" style="position: absolute; z-index: -1; visibility: hidden;">`
<span class="jquery-safari-checkbox jquery-safari-checkbox-checked"><span class="mark"><img src="empty.png"></span></span>

thus leaving me with no way to see if the input is checked. 因此让我无法看到输入是否被检查。 Any ideas on what I can do (if anything) or shall i just revert to the old checkboxes? 关于我能做什么(如果有的话)的任何想法,或者我只是回到旧的复选框?

Kinda messy in my opinion to do this this way, but: 在我看来有点混乱这样做,但是:

if($(".jquery-safari-checkbox").hasClass("jquery-safari-checkbox-checked")){
   //Do something
   return false;
}

You don't have to do anything special. 你不需要做任何特别的事情。 The original checkbox is still there and maintains its state. 原始复选框仍然存在并保持其状态。 When you click the fancy checkbox, the plugin updates the hidden checkbox to match the current state. 单击花式复选框时,插件会更新隐藏的复选框以匹配当前状态。

You can update your code a little, too, if you use the latest jQuery. 如果使用最新的jQuery,也可以稍微更新一下代码。 Your code is quite antiquated: 你的代码很陈旧:

$('#Button').click(function () {
    if (!$('input[name=image_id]').prop('checked')) {
        //Do something
        return false;
    }
});

Demo: http://jsfiddle.net/k2d6E/ 演示: http//jsfiddle.net/k2d6E/

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

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