关于根据另一组复选框禁用一组复选框

About disabling a group of checkbox according to another

本文关键字:一组 复选框 于根      更新时间:2023-09-26

在这里的第一篇文章,我会尽量简单:我需要创建一个表格,让医疗会议的参与者注册到课程,每节课持续3个小时,所以想要参加上午10点开始的课程的人不能注册到上午11点,下午12点和下午1点的课程,但他可以注册到下午2点,等等。

每小时有3节不同的课。

我认为这样做使用javascript,但我绝对迷路了。

你能给我提示一下如何做这件事吗?

谢谢! !

快速简单的方法是设置一个javascript函数来运行onChange,禁用或启用其他复选框。你可以这样写

 <input type="checkbox" onChange="enableDisable(11)" name="11" id="whatever">

javascript的伪代码应该是:

 //Declare function
 //Check the current id and get the next 3 IDs
 //Check the status of the currentid to find out if you checked or unchecked it
 //if you checked it, disable the next 3 ids
 //if you unchecked it, enable the next 3, dependant on whether any of the previous 3 values are checked

最后一步可能有点麻烦,但这是我处理它的基本方法

你的问题要求中漏掉了一些重要的点。当用户在上午10点点击注册课程时,他们无法注册两组课程:

  • 第1组:11am - 12am - 1pm
  • 组2:9am - 8am - 7am

正如你所说的,你对javascript几乎是"零"的,我写这些代码只是为了概念目的。

你需要做的第一件事是下载jquery.js文件(你可以在谷歌上找到它),然后把这个文件和你的register.php文件放在同一个目录下。

代码如下:

<input type="checkbox" value="10" name="convention_time"> 10 am<br>
<input type="checkbox" value="11" name="convention_time"> 11 am<br>
<input type="checkbox" value="12" name="convention_time"> 12 am<br>
<input type="checkbox" value="13" name="convention_time"> 1 pm<br>
<input type="checkbox" value="14" name="convention_time"> 2 pm<br>
<input type="checkbox" value="15" name="convention_time"> 3 pm<br>
<script>
$(function(){
    $('[name="convention_time"]').live('click',function(){
        var A=$(this);
        var method2call = A.is(':checked')?'attr':'removeAttr'
        var selector = '[name="convention_time"]:lt(3)'
        A.nextAll(selector).add( A.prevAll(selector) )[method2call]('disabled','disabled').attr('checked',false)
    })
})
</script>