【问题标题】:Get check box value if checked如果选中,则获取复选框值
【发布时间】:2013-08-22 14:14:06
【问题描述】:

我正在使用带有 JQuery mobile 的 HTML 构建一个表单,以便可以在移动设备上使用该表单。

我通过电子邮件将表单导出为 CSV。但是,如果未选中复选框,则不会写入 CSV 文件。

我可以使用 jQuery 中的函数从选中的复选框中提取值,使用标签中的值,并将未选中的框标记为NullSpace,以便当我将它们导入 Excel 时注意到没有检查值?

<label for="question4" class="input"> 4. What language (s) are you currently using? </label>
<fieldset data-role="controlgroup">
    <legend>Multi select:</legend>
    <input type="checkbox" name="checkbox-1" id="checkbox-1" class="custom" value="english"/>
    <label for="checkbox-1"> English</label>
    <input type="checkbox" name="checkbox-2" id="checkbox-2" class="custom" />
    <label for="checkbox-2">French</label>
    <input type="checkbox" name="checkbox-3" id="checkbox-3" class="custom" />
    <label for="checkbox-3">Spanish</label>
    <input type="checkbox" name="checkbox-4" id="checkbox-4" class="custom" />
    <label for="checkbox-4">Brazilian Portuguese</label>
    <input type="checkbox" name="checkbox-5" id="checkbox-5" class="custom" />
    <label for="checkbox-5">Italian</label>
    <input type="checkbox" name="checkbox-6" id="checkbox-6" class="custom" />
    <label for="checkbox-6">German</label>
    <input type="checkbox" name="checkbox-7" id="checkbox-7" class="custom" />
    <label for="checkbox-7">Japanese</label>
</fieldset>
</br>

如果在 JQuery 中没有办法,那么在 PHP 中呢?由于我使用 PHP 来填充 CSV 文件。

会不会是某种形式的 if 语句:

if checkbox = yes then pull value from <label>

【问题讨论】:

    标签: php javascript jquery html


    【解决方案1】:

    更改您的 HTML 并为所有复选框赋予 name 属性,如下所示:

    <form action="" method="post">
    <label for="question4" class="input"> 4. What language (s) are you currently using? </label>
    <fieldset data-role="controlgroup">
    <legend>Multi select:</legend>
    <input type="checkbox" name="checkbox[]" id="checkbox-1" class="custom" value="english"/>
    <label for="checkbox-1"> English</label>
    <input type="checkbox" name="checkbox[]" id="checkbox-2" class="custom" />
    <label for="checkbox-2">French</label>
    <input type="checkbox" name="checkbox[]" id="checkbox-3" class="custom" />
    <label for="checkbox-3">Spanish</label>
    <input type="checkbox" name="checkbox[]" id="checkbox-4" class="custom" />
    <label for="checkbox-4">Brazilian Portuguese</label>
    <input type="checkbox" name="checkbox[]" id="checkbox-5" class="custom" />
    <label for="checkbox-5">Italian</label>
    <input type="checkbox" name="checkbox[]" id="checkbox-6" class="custom" />
    <label for="checkbox-6">German</label>
    <input type="checkbox" name="checkbox[]" id="checkbox-7" class="custom" />
    <label for="checkbox-7">Japanese</label>
    </fieldset>
    </br>
    <input type="submit" name="submit">
    </form>
    

    提交表单后,名称属性将作为数组传递,并且可以使用相同的变量访问它们。现在,您可以使用isset() 来检查是否设置了特定项目:

    if(isset($_POST['checkbox'])) {
      print_r($_POST); //print all checked elements
    }
    

    如果要获取被选中的复选框的数量,可以使用count()

    $number = count($_POST['checkbox']);
    

    注意:目前,只有第一个元素具有value 属性。您必须为其余的复选框添加它才能正常工作。

    希望这会有所帮助!

    【讨论】:

    • 这真的很有帮助!我想知道是否有办法让我不必添加名称而只需使用我所拥有的?
    • @user1292136:当然可以。这只是推荐的做法,但如果你想要那样做,肯定可以做到。像if(isset($_POST['checkbox-1']) { ... } 这样的东西应该可以工作。
    • 太棒了!这会拉动标签的价值吗?所以如果我选择了“英语、意大利语和日语”,它会打印英语、意大利语、日语吗?
    • @user1292136:是的,它将获得value 属性。在上面的 HTML 中,只有 English 具有 value 属性。您必须为其余元素添加它,它会按照您的描述工作。
    • 啊!有没有办法从
    【解决方案2】:
      foreach($_POST['checkbox'] as $value)
      {
         echo 'checked: '.$value.'';
      }
    

    【讨论】:

      【解决方案3】:

      快速查看此答案以检查是否选中了复选框。

      How to check whether a checkbox is checked in jQuery?

      但基本上你想做如下的事情来检查它的价值:

      if ($("#element").is(":checked")) {
        alert("I'm checked");
      }
      

      【讨论】:

      • 感谢您的超快速回复!因此,使用该 if 语句,我是否必须执行一个循环来计算检查了多少个复选框?我可以做一个通用命令来遍历整个表单,然后将其输入到 .csv 中,我想我现在很困惑。
      • 看看这里的官方文档:api.jquery.com/checked-selector 但是基本上看看我这里的jsfiddle,它应该做你想做的:jsfiddle.net/fdCba
      • 非常感谢,上面的回答对您很有帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      相关资源
      最近更新 更多