检测单击复选框的顺序 [英] Detect order in which checkboxes are clicked

查看:108
本文介绍了检测单击复选框的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要创建一个页面,允许用户从总共25个复选框中选择8个复选框。



我想知道,如何检测确切的顺序在其中检查。我使用一个简单的html首页,将通过指向一个php页面的表单操作验证。



我想获得一个结果像(checkbox1,checkbox2,checkbox6 ,checkbox3,checkbox7等)为八个复选框,以及他们被点击的确切顺序。



我想我已经找到了我在找什么,我不是太确实,但我有麻烦实现它。



这是我到目前为止,我想我的问题是,我需要什么类型的PHP我需要收集这个信息一次

 <$ c $ 

c>< form id =form1name =form1method =postaction =check_combination.php>
< label id =lblA1>< / label>
< input name =checkbox1type =checkboxvalue =a1onclick =setChecks(this)/>选项1
< label id =lblA2>< / label>
< input name =checkbox1type =checkboxvalue =a2onclick =setChecks(this)/>选项2
< label id =lblA3>< / label>
< input name =checkbox1type =checkboxvalue =a3onclick =setChecks(this)/>选项3
< label id =lblA4>< / label>
< input name =checkbox1type =checkboxvalue =a4onclick =setChecks(this)/>选项4
< / form>

对于Javascript,我有:

 < script type =text / javascript> 
<! -
//初始checkCount为零
var checkCount = 0

//允许的复选框的最大数量
var maxChecks = 8

function setChecks(obj){
//增加/减少checkCount
if(obj.checked){
checkCount = checkCount + 1
} else {
checkCount = checkCount-1
}
//如果他们选中第4个框,取消选中该框,然后递减checkcount和pop alert
if(checkCount> maxChecks){
obj.checked = false
checkCount = checkCount-1
alert('你最多只能选择'+ maxChecks +'选项')
}
}
// - >
< / script>

< script type =text / javascript>
<! -
$(document).ready(function(){
var array = [];
$('input [name =checkbox1]' ).click(function(){
if($(this).attr('checked')){
//如果选中,添加新元素:
array.push这个).attr('value'));
}
else {
//如果未选中则删除元素:
for(var i = 0; i if(array [i] == $(this).attr('value')){
array.splice(i,1);
}
}
}
//清除所有标签:
$(label)每个(function(i,elem){
$(elem).html ;
});
//检查数组和更新标签
for(var i = 0; i if i == 0){
$(#lbl+ array [i] .toUpperCase())。html(first);
} {
$(#lbl+ array [i] .toUpperCase())。html(second);
}
}
});
});
// - >
< / script>

我已经得到只允许8个复选框被选中的部分,需要做的事情,一旦它已经提交到一个名称像check_combination.php的页面,实际上解析数据。



我非常感谢任何帮助

解决方案


  • 使用

  • 命令创建隐藏的输入字段

  • 您将拥有可以由PHP处理的订单


I am trying to make a page that allows users to select 8 checkboxes from a total of 25.

Im wondering, how to detect the exact order in which they check them. I am using a plain html front page that will be verified by a form action pointing to a php page.

Im trying to get a result like (checkbox1,checkbox2,checkbox6,checkbox3,checkbox7,etc) for eight checkboxes, and the exact order in which they were clicked.

I think I have found what I am looking for,Im not too sure, but Im having trouble implementing it.

This is what I have so far, I guess my question is, what type of php do I need to gather this info once a user has submitted the form.

For the form I have:

<form id="form1" name="form1" method="post" action="check_combination.php">
    <label id="lblA1"></label> 
    <input name="checkbox1" type="checkbox" value="a1" onclick="setChecks(this)"/> Option 1 
    <label id="lblA2"></label> 
    <input name="checkbox1" type="checkbox" value="a2" onclick="setChecks(this)"/> Option 2 
    <label id="lblA3"></label> 
    <input name="checkbox1" type="checkbox" value="a3" onclick="setChecks(this)"/> Option 3 
    <label id="lblA4"></label> 
    <input name="checkbox1" type="checkbox" value="a4" onclick="setChecks(this)"/> Option 4
</form>

For the Javascript I have:

<script type="text/javascript">
<!--
//initial checkCount of zero
var checkCount=0

//maximum number of allowed checked boxes
var maxChecks=8

function setChecks(obj){
//increment/decrement checkCount
if(obj.checked){
checkCount=checkCount+1
}else{
checkCount=checkCount-1
}
//if they checked a 4th box, uncheck the box, then decrement checkcount and pop alert
if (checkCount>maxChecks){
obj.checked=false
checkCount=checkCount-1
alert('you may only choose up to '+maxChecks+' options')
}
}
//-->
</script>

<script type="text/javascript">
<!--
$(document).ready(function () { 
    var array = []; 
    $('input[name="checkbox1"]').click(function () { 
        if ($(this).attr('checked')) { 
            // Add the new element if checked: 
            array.push($(this).attr('value')); 
        } 
        else { 
            // Remove the element if unchecked: 
            for (var i = 0; i < array.length; i++) { 
                if (array[i] == $(this).attr('value')) { 
                    array.splice(i, 1); 
                } 
            } 
        } 
        // Clear all labels: 
        $("label").each(function (i, elem) { 
            $(elem).html(""); 
        }); 
        // Check the array and update labels. 
        for (var i = 0; i < array.length; i++) { 
            if (i == 0) { 
                $("#lbl" + array[i].toUpperCase()).html("first"); 
            } 
            if (i == 1) { 
                $("#lbl" + array[i].toUpperCase()).html("second"); 
            } 
        } 
    }); 
});
//-->
</script>

I have gotten the part that only allows 8 checkboxes to be checked, but Im stuck as to what I need to do to actually parse the data once it has been submitted to a page with a name like check_combination.php.

I would appreciate any help

解决方案

  • create a hidden input field with the order
  • update this input field when something changes
  • you'll have the order ready to be processed by PHP

这篇关于检测单击复选框的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆