PHP 或 JS GET 表单:如果选中复选框,如何添加字段

PHP or JS GET form: How to add a field if checkbox is checked

我有包含多个复选框字段的基本 html 表单。如果选中复选框,我想添加另一个字段,以便通过 url 传递添加的字段。到目前为止我的代码,但它不工作:

<form action="/s/mypage/" method="GET">
<input name="id1" type="checkbox" value="ime" checked/>Input 1
<input name="id11" type="checkbox" value="deinost" />Input 11
<input name="id2" type="checkbox" value="adres" />Input 2
<input name="id3" type="checkbox" value="kapital" />Input 3
<input name="id4" type="checkbox" value="upravitel" />Input 4
<input name="id5" type="checkbox" value="sobstvenik" />Input 5
<input name="id6" type="checkbox" value="drugo" />Input 6
<input name="" type="submit" value="go ahead" />

<?php
if (isset($_GET["id[1]"])) {
echo '<input type="hidden" name="do" value="12345" />';
}
?>
</form>

我该怎么做?不一定要和php在一起,javascript就可以了。

由于条件取决于复选框状态,您可能希望在客户端的 javascript 中执行此操作。一种方法是在提交表单时进行检查。如果您使用 jQuery,它可能看起来像这样:

$('form').submit(function() {
  if($('#id1').is(':checked')) {
    $(this).append('<input type="hidden" name="do" value="12345" />');
  }
});

您可以尝试以下方法:

<script>
   $(function(){
       $('input[type=checkbox]').on('click', function(){
         if( $(this).is(':checked') ){
            /// your code goes here if a checkbox checked
          }
       })
   }) 
</script>
<table>
<td>
<input type ="checkbox" id ="morning"> Morning</td><td class="hiden css"> After which period want break  </td>
                                    <td> <input type="text" class="hiden css" id="morning_break" pattern ="[0-9]+" name="morning_break"></td>
                  </table>
$(document).ready(function(){
        $(".hiden,.hide1").hide();
        });
   $("#morning").click(function(){
        $(".hiden").toggle();
    });

Here the code works well on your requirement