[英]Get check box value if checked
我正在使用HTML和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>
更改您的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
屬性。 您必須在其余復選框中添加它,才能使其正常工作。
希望這可以幫助!
foreach($_POST['checkbox'] as $value)
{
echo 'checked: '.$value.'';
}
快速查看此答案,以檢查是否已選中復選框。
但基本上,您想執行以下操作來檢查其值:
if ($("#element").is(":checked")) {
alert("I'm checked");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.