php 如果选中,则获取复选框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18382979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get check box value if checked
提问by user1292136
I'm building a form using HTML with JQuery mobile so that the form can be used on mobile devices.
我正在使用带有 JQuery mobile 的 HTML 构建一个表单,以便该表单可以在移动设备上使用。
I have the form exporting to CSV via email. However the write to the CSV file doesn't occur when Checkboxes aren't checked.
我有通过电子邮件导出到 CSV 的表单。但是,当未选中复选框时,不会写入 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
或标记为 aSpace
以便当我将它们导入 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? Since I'm using PHP to populate the CSV file.
如果在 JQuery 中没有办法,那么在 PHP 中呢?因为我使用 PHP 来填充 CSV 文件。
Would it be some form of a if statement that says:
它会是某种形式的 if 语句,它说:
if checkbox = yes then pull value from <label>
回答by Amal Murali
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. You'll have to add it for the rest of the checkboxes for it to work properly.
注意:目前,只有第一个元素有一个value
属性。您必须为其余的复选框添加它才能正常工作。
Hope this helps!
希望这可以帮助!
回答by Deepanshu
foreach($_POST['checkbox'] as $value)
{
echo 'checked: '.$value.'';
}
回答by Mark
Have a quick look at this answer for checking if a checkbox is checked.
快速查看此答案以检查是否选中了复选框。
How to check whether a checkbox is checked in jQuery?
But basically you want to do something like below to check its value:
但基本上你想要做类似下面的事情来检查它的价值:
if ($("#element").is(":checked")) {
alert("I'm checked");
}