我正在使用JQuery mobile构建一个表单,这样表单就可以在移动设备上使用。
我通过电子邮件将表单导出到CSV。但是,如果未选中复选框,则不会写入CSV文件。
我是否可以使用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>如果在JQuery中没有办法做到这一点,那用PHP呢?因为我使用PHP来填充CSV文件。
它会不会是某种形式的if语句,它说:
if checkbox = yes then pull value from <label>发布于 2013-08-22 22:29:53
更改您的超文本标记语言,并为所有的复选框提供一个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>表单提交后,name属性将作为数组传递,并可使用相同的变量进行访问。现在,您可以使用isset()检查是否设置了特定的项目:
if(isset($_POST['checkbox'])) {
print_r($_POST); //print all checked elements
}如果要获取已选中的复选框的数量,可以使用count()
$number = count($_POST['checkbox']);注意:目前,只有第一个元素有value属性。您必须将其添加到其余的复选框中,才能使其正常工作。
希望这能有所帮助!
发布于 2014-04-03 20:07:41
foreach($_POST['checkbox'] as $value)
{
echo 'checked: '.$value.'';
}发布于 2013-08-22 22:15:25
快速查看此答案,以检查是否选中了复选框。
How to check whether a checkbox is checked in jQuery?
但是基本上你想做一些类似下面的事情来检查它的值:
if ($("#element").is(":checked")) {
alert("I'm checked");
}https://stackoverflow.com/questions/18382979
复制相似问题