使用JScript确定条件

时间:2014-04-06 16:22:38

标签: javascript

伙计我的页面最多可以有3个复选框吗?

目前,我有6个复选框..如果我勾选所有6个框...将在顶部显示3个表格,在底部显示3个表格。但是我只想要出现3个最大表格。

因此,我在考虑检查条件 (1)如果仅勾选了1个复选框,则会通知用户必须至少检查2个表才能进行比较.-完成

(2)如果选择了超过3个表。它将通知用户它无法继续并且不会带任何表.-需要帮助

<!DOC HTML>
<html>
<TITLE></TITLE>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
$('input[type="submit"]').click(function () {
if($('input[name=Option]:checked').length >1)
{
     $('.frame-wrapper').fadeOut();
$('input[name=Option]').each(function() {
    if ($(this).prop("checked"))
    {
        $('.frame-wrapper').eq( $(this).index() -1 ).fadeIn();
    }                            
});  
}
else
  {
     alert("You must compare with more than 1 item.!!");
  }
});
        $('input[type="compare"]').click(function () {
           $('.frame-wrapper').eq( $(this).index() -1 ).fadeIn();
        });

    });

</script>
<style type="text/css">
    .frame-wrapper {
        display: none;
        float: left;
        width: 32%;
        margin-top: 20px;
        margin-right: 1%;
        background-color: #eee;
    }
</style>
</head>
<body>
<b>Please select an option</b>
A <input type="checkbox" name="Option" />
B <input type="checkbox" name="Option" />
C <input type="checkbox" name="Option" />
D <input type="checkbox" name="Option" />
E <input type="checkbox" name="Option" />
F <input type="checkbox" name="Option" />
<input type="submit" value="Compare"/>
<input type="reset" name="clear" value="Clear"/>

<div style="clear: both;"></div>
<div id="tblA" class="frame-wrapper">
    You selected A
</div>
<div id="tblB" class="frame-wrapper">
    You selected B
</div>
<div id="tblC" class="frame-wrapper">
    You selected C
</div>
<div id="tblD" class="frame-wrapper">
    You selected D
</div>
<div id="tblE" class="frame-wrapper">
    You seleted E
</div>

<div id="tblF" class="frame-wrapper">
    You selected F
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

在输入复选框中添加一个类(例如.foo)。

然后你可以做

 $('.foo').click(function()
{
//every time a checkbox is clicked you can run your sum checks.
//you can loop over all '.foo' classes and check how many were checked.
//then run an if statement to more dirty work.


});