[英]AJAX POST not sending all data
我在表單中有以下 HTML 代碼,頂部的(政策)在帖子中發送,但第二個沒有(mfa)。 我不知道為什么會這樣,有什么想法嗎?
<label class="switch switch-flat">
<input class="switch-input" type="checkbox" id="policy" method="post" name="policy" <?php if($row['policy'] === '1') echo 'checked="checked"';?> />
<span class="switch-label" data-on="Yes" data-off="No"></span>
<span class="switch-handle"></span>
</label>
<label for="policy">Does the vendor organization have a written Information Security policy?</label>
</td>
</tr>
<tr>
<td>
<div class="col-md-12">
<label class="switch switch-flat">
<input class="switch-input" type="checkbox" name="mfa" id="mfa" method="post" <?php if($row['mfa'] === '1') echo 'checked="checked"';?> />
<span class="switch-label" data-on="Yes" data-off="No"></span>
<span class="switch-handle"></span>
</label>
<label for="mfa">Do they support and utilize multi-factor authentication?</label>
</td>
這是我發送帖子數據的ajax。 當我在 Dev 工具中瀏覽要發送的標頭時,我看到許多其他 HTML 名稱正在運行,但除了最上面的“策略”之外,大多數列為切換復選框的名稱都沒有發送。
<script type='text/javascript'>
/* attach a submit handler to the form */
$('#update').submit(function(e){
e.preventDefault();
$.ajax({
url:'updateVendor.php',
type:'post',
data:$('#update').serialize(),
success:function(){
//whatever you wanna do after the form is successfully submitted
}
});
});
</script>
復選框僅在被選中時才被認為具有值。 否則,它不會包含在提交的表單數據中。
從MDN 網站:
注意:如果一個checkbox在提交表單時未勾選,則沒有提交給服務器的值代表其未勾選狀態(例如value=unchecked); 該值根本不提交給服務器。 如果您想在未選中時為復選框提交默認值,您可以在表單中包含一個
<input type="hidden">
具有相同name
和value
,可能由 JavaScript 生成。
規避此限制的一個技巧是包含具有默認值的隱藏輸入。 例如
<input name="mfa" type="hidden" value="0">
<input name="mfa" type="checkbox" value="1">
使用這種方法, mfa
的值將始終與表單數據一起提交。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.