简体   繁体   English

如果选中,则获取复选框值

[英]Get check box value if checked

I'm building a form using HTML with JQuery mobile so that the form can be used on mobile devices. 我正在使用HTML和JQuery mobile构建表单,以便可以在移动设备上使用该表单。

I have the form exporting to CSV via email. 我有通过电子邮件导出到CSV的表格。 However the write to the CSV file doesn't occur when Checkboxes aren't checked. 但是,如果未选中复选框,则不会写入CSV文件。

Can I use functions in jQuery to pull the values from the checked checkboxes, using the values from the label, and to mark the unchecked boxes as Null or as a Space so that when I import them into Excel it notices the values aren't checked? 我可以使用jQuery中的函数来从复选框中拉出选中的值,使用标签中的值以及将未选中的框标记为Null还是Space以便在我将其导入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>

if there isn't a way to do in JQuery, what about in PHP? 如果在JQuery中没有办法,那么在PHP中呢? Since I'm using PHP to populate the CSV file. 由于我使用PHP填充CSV文件。

Would it be some form of a if statement that says: if语句的某种形式是否表示:

if checkbox = yes then pull value from <label>

Change your HTML and give a name attribute to all your checkboxes, like so: 更改您的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>

Once the form has been submitted, the name attributes will be passed as array and they'll be accessible using the same variable. 提交表单后,名称属性将作为数组传递,并且可以使用相同的变量进行访问。 Now, you can use isset() to check if a particular item was set: 现在,您可以使用isset()检查是否设置了特定项目:

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

If you want to get the number of checkboxes that were checked, you can use count() : 如果要获取已选中的复选框数,则可以使用count()

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

Note: currently, only the first element has a value attribute. 注意:目前,只有第一个元素具有value属性。 You'll have to add it for the rest of the checkboxes for it to work properly. 您必须在其余复选框中添加它,才能使其正常工作。

Hope this helps! 希望这可以帮助!

  foreach($_POST['checkbox'] as $value)
  {
     echo 'checked: '.$value.'';
  }

Have a quick look at this answer for checking if a checkbox is checked. 快速查看此答案,以检查是否已选中复选框。

How to check whether a checkbox is checked in jQuery? 如何检查jQuery中是否已选中复选框?

But basically you want to do something like below to check its value: 但基本上,您想执行以下操作来检查其值:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM