【问题标题】:Need to require all check box goup until one is selected需要所有复选框组,直到选中一个
【发布时间】:2011-03-31 18:47:16
【问题描述】:

我正在使用 JQM (jQueryMobile) 并且需要要求一个组中的所有复选框,直到选中一个,然后只要求选中的一个,除非它是选中所有然后要求所有。

问题是我不能使用复选框组的Id或Name属性,所以我尝试使用class属性。

这是一个标记示例(但功能不起作用):http://jsfiddle.net/HfLq3/7/

HTML:

<div data-role="page"> 
    <form id="chips_form" name="chips_form" action="#" method="post">
        <div  data-role="fieldcontain"> 
         <fieldset data-role="controlgroup"> 
            <legend>Choose as many snacks as you'd like:</legend> 
            <input type="checkbox" name="checkbox-1a" id="checkbox-1a" class="custom chips" /> 
            <label for="checkbox-1a">Cheetos</label> 

            <input type="checkbox" name="checkbox-2a" id="checkbox-2a" class="custom chips" /> 
            <label for="checkbox-2a">Doritos</label> 

            <input type="checkbox" name="checkbox-3a" id="checkbox-3a" class="custom chips" /> 
            <label for="checkbox-3a">Fritos</label> 

            <input type="checkbox" name="checkbox-4a" id="checkbox-4a" class="custom chips" /> 
            <label for="checkbox-4a">Sun Chips</label> 

            <input type="checkbox" name="checkbox-5a" id="checkbox-5a" class="custom chips" /> 
            <label for="checkbox-5a">Select All</label> 
        </fieldset> 
        </div> 
        <div data-role="fieldcontain">
            <button type="submit"  name="submit" value="chips-submit">Submit</button>
        </div>
    </form>
</div> 

JS:

$(".chips").change(function() {
    alert('selected Value(s): '+$(this).val());
});

$('.chips').each(function() {
    $(this).attr('required','required');
});

【问题讨论】:

    标签: jquery html checkbox jquery-mobile


    【解决方案1】:
    $(".chips").change(function() {
        $('.chips').attr('required', $(this).attr("checked") ? '': 'required');
    });
    
    $('.chips').attr('required','required');
    

    【讨论】:

    • 它可以工作,除非我检查然后取消检查,表单仍然提交而没有检查
    猜你喜欢
    • 2012-05-31
    • 2016-05-29
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多