【问题标题】:Check if at least one checkbox is selected PER questio.n检查是否至少选择了一个复选框 PER questio.n
【发布时间】:2012-04-11 04:07:50
【问题描述】:

我们又来了,搜索并尝试了自己,但没有一个在线示例与我的情况不同。 我有 2 个基于无线电的问题和其他 22 个带有复选框的问题。 我的问题是如何检查是否在每个问题上至少选择了 1 个复选框/收音机并显示 javascript 警报以防它们没有 EG : “你必须在第 3 题上至少选择一个答案”。

注意:警报必须包含问题编号。 CHECK 必须在表单提交时完成(如果失败将返回 false ,否则将返回 true 并将被处理。)

下面的例子:

<tr><th>- Question 1</th></tr>
<tr><td><input name="01" type="radio" id="01" value="F">
F</td></tr>
<tr><td><input name="01" type="radio" id="01" value="B">
B</td></tr>

<tr><th>- Question 2</th></tr>
<tr><td><input name="02" type="radio" id="02" value="9">
9</td></tr>
<tr><td><input name="02" type="radio" id="02" value="10">
10</td></tr>
<tr><td><input name="02" type="radio" id="02" value="11">
11</td></tr>
<tr><td><input name="02" type="radio" id="02" value="12">
12</td></tr>

<tr><th> Question 3</tr></th>
<tr><td><input name="c1" type="checkbox" id="c1" value="a">
This is checkbox1</td></tr>
<tr><td><input name="c1" type="checkbox" id="c1" value="b">
This is checkbox2</td></tr>

【问题讨论】:

  • 您有多个具有相同 id 的元素。这是不允许的。
  • @Quentin 据我了解,他们必须是这样才能对他们进行分组。
  • @virusbogdan — 不,它们必须具有相同的 name。他们的 ID 仍然需要是唯一的。
  • @Quentin 我现在就解决这个问题!

标签: php javascript html checkbox radio


【解决方案1】:

EDIT - 看到标记后编辑

$('form').submit(function() {
    var ok = true;
    var that = this;
    //get the <tr> inside the function, i assume thatyou have one question per <tr>
    $('tr th', this).each(function(idx, el) {
        var id = this.id;
        if (id) {
            if ($('input:checked[name=' + id + ']', that).length === 0) {
                alert("You must check at least one option for question " + $(this).text());
                ok = false;
            }
        }
    });
    if (ok === false) {
        return false;
    }
});

这是我使用的标记(这里小提琴http://jsfiddle.net/R7uwa/2/

<form id="chestionar" name="chestionar" method="post" action="prelucrare_chest.php">

    <table style="width:100%; border-spacing:0;">
    <tbody><tr><th>- Selectati sex-ul:</th></tr>
    <tr><td><input type="radio" value="F" id="01" name="01">
    Fata</td></tr>
    <tr><td><input type="radio" value="B" id="01" name="01">
    Baiat</td></tr>

    <tr><th>- Selectati clasa:</th></tr>
    <tr><td><input type="radio" value="9" id="02" name="02">
    9</td></tr>
    <tr><td><input type="radio" value="10" id="02" name="02">
    10</td></tr>
    <tr><td><input type="radio" value="11" id="02" name="02">
    11</td></tr>
    <tr><td><input type="radio" value="12" id="02" name="02">
    12</td></tr>

    <tr><th id="c1">1. Care sunt motivele pentru care absentezi tu si colegii tai?</th></tr>
    <tr><td><input type="checkbox" value="a" id="c1" name="c1">
    Nu ne place ora</td></tr>
    <tr><td><input type="checkbox" value="b" id="c1" name="c1">
    Nu ne intereseaza scoala</td></tr>
    <tr><td><input type="checkbox" value="c" id="c1" name="c1">
    Nu ne place profesorul</td></tr>
    <tr><td><input type="checkbox" value="d" id="c1" name="c1">
    Daca absentam nu ni se întâmpla mare lucru</td></tr>
    <tr><td><input type="checkbox" value="e" id="c1" name="c1">
    Ne plictisim la scoala</td></tr>
    <tr><td><input type="checkbox" value="f" id="c1" name="c1">
    Facem usor rost de scutiri pentru motivarea absentelor</td></tr>
    <tr><td><input type="checkbox" value="g" id="c1" name="c1">
    Evitam în felul acesta luarea unei note mici</td></tr>
    <tr><td><input type="checkbox" value="h" id="c1" name="c1">
    Nu dorim sa intram în conflict cu ceilalti colegi</td></tr>
    <tr><td><input type="checkbox" value="i" id="c1" name="c1">
    Programul este prea încarcat</td></tr>



    <tr><th id='c2'>2. Ce se întâmpla când un elev absenteaza nemotivat mai mult timp?</th></tr>
    <tr><td><input type="checkbox" value="a" id="c2" name="c2">
    I se scade nota la purtare</td></tr>
    <tr><td><input type="checkbox" value="b" id="c2" name="c2">
    Sunt chemati parintii lui la scoala</td></tr>
    <tr><td><input type="checkbox" value="c" id="c2" name="c2">
    Este mustrat de catre diriginte</td></tr>
    <tr><td><input type="checkbox" value="d" id="c2" name="c2">
    Este evaluat la ora urmatoare si este depunctat la nota</td></tr>
    <tr><td><input type="checkbox" value="e" id="c2" name="c2">
    Este exmatriculat</td></tr>
    <tr><td><input type="checkbox" value="f" id="c2" name="c2">
    Nu se întâmpla mare lucru</td></tr>

    <tr><th id='c3'>3. Cât de des absentezi nemotivat?</th></tr>
    <tr><td><input type="checkbox" value="a" id="3" name="3">
    Deloc</td></tr>
    <tr><td><input type="checkbox" value="b" id="c3" name="c3">
    1-3 ore pe saptamâna</td></tr>
    <tr><td><input type="checkbox" value="c" id="c3" name="c3">
    3-6 ore pe saptamâna</td></tr>
    <tr><td><input type="checkbox" value="d" id="c3" name="c3">
    6-9 ore pe saptamâna</td></tr>
    <tr><td><input type="checkbox" value="e" id="c3" name="c3">
    9-12 ore pe saptamânat</td></tr>
    <tr><td><input type="checkbox" value="f" id="c3" name="c3">
    12-15 ore pe saptamâna</td></tr>
    <tr><td><input type="checkbox" value="g" id="c3" name="c3">
    Peste 15 ore pe saptamâna</td></tr>
    </tbody></table>
    <div class="form_settings"><p style="padding-top: 15px"><span>&nbsp;</span><input type="submit" value="Trimite" name="contact_submitted" class="submit"></p></div></form>

【讨论】:

  • 我只是在选中/取消选中复选框时收到随机警报。
  • 您可以发布您的表单的完整 html 吗?
  • link 哦,前两个单选选项不是问题。我不希望他们进入 'idx' 计数。
  • @virusbogdan 我可以在你的标记中添加 id 吗?我只需要为每个问题添加一个与问题名称相同的 id
  • @virusbogdan 只有问题有一个 id,我用那个 id 来检查是否选中了同名的复选框,这真的很基本
【解决方案2】:

输入名称="c1[]" type="checkbox" id="c1" value="a"

输入名称="c1[]" type="checkbox" id="c1" value="b"

$(document).ready(function() { $('form').submit(function(){ var checkbox_value = []; $('#c1:checked').each(function(i){ checkbox_value[i] = $(this).val(); }); if(checkbox_value==""){ alert("Please Check Minimum One"); return false; } }

`

【讨论】:

    猜你喜欢
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 2015-06-23
    相关资源
    最近更新 更多