简体   繁体   English

选中初始复选框时(如何使用PHP / Javascript)如何显示多个复选框

[英]How can I have multiple checkboxes appear when initial checkbox is checked (using PHP/Javascript)

I am trying to implement a small part of my program where when an initial checkbox is clicked, it would open multiple checkboxes are opened up for selection. 我正在尝试实现程序的一小部分,当单击初始复选框时,它将打开多个复选框供选择。 I don't want to use forloop (or dynamically) to create the multiple checkboxes but I need to manually create them. 我不想使用forloop(或动态地)创建多个复选框,但我需要手动创建它们。

My program is below and I am not sure why it doesn't work. 我的程序在下面,我不确定为什么它不起作用。 If someone can kindly pleaes point me to my mistake, I would greatly appreciate. 如果有人能请我指出我的错误,我将不胜感激。 I am not skilled with PHP/JavaScript. 我不擅长PHP / JavaScript。

Thanks! 谢谢!

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
 <script type="text/javascript">
    $(document).ready(function() {
    //set initial state.
    $('#checkbox').val($(this).is(':unchecked'));

    $('#checkbox').change(function() {
        if($(this).is(":checked")) {
            var box = document.createElement("div");
            box.innerHTML = <input type="chkbox" name="checkme"> 2nd checkbox;
            document.myForm.appendChild(box);
            hasBox = true;
        }  
    });
});
</script>
</head>
<body>

<p>Click on this paragraph.</p>
<form action="">
<input id="checkbox" name="click" type="checkbox" onclick="check(this)"/>initial checkbox<br>

</body>
</html> 

Your on the right track. 您走对了。

You have a few problems in your code. 您的代码中有一些问题。

1) You forgot to enclose your new checkbox tag within quotation marks. 1)您忘记将新的复选框标签括在引号中。

    box.innerHTML = <input type="chkbox" name="checkme"> 2nd checkbox;

should be: 应该:

    box.innerHTML = "<input type=\"checkbox\" name=\"checkme\"> 2nd checkbox<br>";

Also note the change from type "chkbox" to "checkbox" 另请注意,从类型“ chkbox”更改为“复选框”

2) To set the initial state for a checkbox I would use the inbuilt prop function in JQuery. 2)要设置复选框的初始状态,我将使用JQuery中的内置prop函数。 For example: 例如:

    $('#checkbox').prop('checked', false);

rather than your code of: 而不是您的代码:

    $('#checkbox').val($(this).is(':unchecked'));

3) The last problem is when you append the new checkbox to the form. 3)最后一个问题是当您将新复选框附加到表单时。 The way that i would do this is using JQuery again: 我这样做的方式是再次使用JQuery:

    $("#myForm").append(box);

and give the form element an id of "myForm" 并将表单元素的ID设置为“ myForm”

Please find below my full code which works to your requirements: 请在下面找到符合您要求的完整代码:

 $(document).ready(function() { //set initial state. $('#checkbox').prop('checked', false); $('#checkbox').on('click', function() { if($(this).is(":checked")) { var box = document.createElement("div"); box.innerHTML = "<input type=\\"checkbox\\" name=\\"checkme\\"> 2nd checkbox<br>"; $("#myForm").append(box); hasBox = true; } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <p>Click on this paragraph.</p> <form action="" id="myForm"> <input id="checkbox" name="click" type="checkbox"/>initial checkbox<br> </form> 

Hope that you found this useful. 希望您觉得这有用。

You can also do this with CSS: 您也可以使用CSS执行此操作:

 .secondary-checkboxes { display: none; } .primary-checkbox:checked ~ .secondary-checkboxes { display: initial; } 
 <input type="checkbox" class="primary-checkbox"> Primary checkbox <br> <div class="secondary-checkboxes"> <input type="checkbox"> Secondary checkbox 1 <br> <input type="checkbox"> Secondary checkbox 2 <br> <input type="checkbox"> Secondary checkbox 3 <br> </div> 

Source: this Stack Overflow question 资料来源: 这个堆栈溢出问题

暂无
暂无

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

相关问题 选中多个复选框时的javascript代码,表单字段应根据复选框显示(我使用选择选项方法对此进行了尝试) - javascript code when multiple checkboxes are checked form fields should appear according to the checkboxes(i tried this using select option method) 如何在选中单选按钮时取消选中复选框,以及在使用Javascript选中复选框时如何取消选中单选按钮 - How to uncheck checkbox when radio button is checked and uncheck radio button when checkboxes are checked using Javascript 选中另一个复选框后,如何检查该复选框? [JavaScript的] - How can I check a checkbox when another checkbox is checked? [JavaScript] 如果表单中有多个复选框,如何通过当前选中的所有复选框? - If I have multiple checkboxes in a form how can I pass all the checkboxes that are currently checked? 当使用jquery或javascript检查2个复选框时,如何过滤div列表? - How can i filter a list of divs when 2 checkboxes are checked using jquery or javascript? 我可以通过javascript检查多个复选框 - Can I pass multiple checkbox checked with javascript 选中复选框时启用多个复选框 - enable multiple checkboxes when a checkbox is checked 如何在选中Checkbox时调用javascript函数 - How can I call a javascript function when Checkbox is checked 在使用 PHP 发送之前,如何要求检查复选框? - How can I require checkbox to be checked before sending using PHP? 在ASP.NET中使用JavaScript进行检查时,如何传递复选框的值和对应名称? - How can I pass the value and corresponding name of a checkbox when checked using javascript in ASP.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM