简体   繁体   English

复选框二维数组

[英]checkbox 2d array

I have a very tricky situation. 我的处境非常棘手。 I have a list of skills each with a level of 1,2,3 which are represented as check boxes besides each skill name. 我有一个技能列表,每个技能的级别为1,2,3,除每个技能名称外,还以复选框表示。 These skills are pulled out of a database. 这些技能已从数据库中撤出。 Without skill levels, the normal thing would be to assign the value of the checkbox to be the ID of the skill ( which goes into the checkbox array ). 没有技能水平,通常的事情是将复选框的值分配为技能的ID(该ID进入复选框数组)。 Is there any way to store the values in a 2 dimensional array so that I can access the skill which was checked and also the skill level that was checked? 有什么方法可以将值存储在二维数组中,以便我可以访问所检查的技能以及所检查的技能水平? I also need to validate these via Javascript so that the maximum skill level of all the skills that the user chooses does not exceed 5. 我还需要通过Javascript进行验证,以使用户选择的所有技能的最高技能水平不超过5。

This is the code I have written but I cannot retrieved the checked values via Javascript: 这是我编写的代码,但无法通过Javascript检索检查的值:

$sql = "select * from Skill where Active=1";
    $res = mysql_query($sql);
    $count=0;
    echo '<tr>';
    while ($rr=mysql_fetch_assoc($res))
    {
    echo '<td>';
    echo "<input type=\"checkbox\" name=\"skill[][".$rr['SkillID']."]\" value=\"1\" /><span></span></input>";
    echo "<input type=\"checkbox\" name=\"skill[][".$rr['SkillID']."]\" value=\"2\" /><span></span></input>";
    echo "<input type=\"checkbox\" name=\"skill[][".$rr['SkillID']."]\" value=\"3\" /><span>".$rr['Name']."</span></input>";
    echo '</td>';
    $count++;
    if ($count % 3 == 0)
    {
    echo '</tr><tr>';
    }
    }

Any help would be much appreciated. 任何帮助将非常感激。

Thanks! 谢谢!

Instead of 代替

<input ... name=\"skill[][".$rr['SkillID']."\" ... />

Try 尝试

<input ... name=\"skill[]\" id=\"skill_".$rr['SkillID']."\" value=\"1" ... />

Then, to determine the total skill level checked, you can use jQuery: 然后,要确定检查的总技能水平,可以使用jQuery:

<script language="javascript">
    $("input[name=skill[]]").click(function() {
        var total = 0;
        $("input[name=skill[]]:checked").each(function() {
            total += $(this).val();
        });
        if (total > 5) {
            alert("Skill level too high.");
        }
    });
</script>

How about this: 这个怎么样:

<input name="skill[SKILL_ID][1]" value=1>
<input name="skill[SKILL_ID][2]" value=1>
<input name="skill[SKILL_ID][3]" value=1>
.
.
.
<input name="skill[NEXT_SKILL_ID][1]" value=1>
<input name="skill[NEXT_SKILL_ID][2]" value=1>
<input name="skill[NEXT_SKILL_ID][3]" value=1>

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

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