[英]How do I remove value from POST if checkbox is unchecked
jQuery的
$("input[type=checkbox]:checked").live("click", function (e) {
var data = $('.variation:checked').map(function (i, n) {
return {
'val': $(n).val()
};
}).get();
$.post('/variations.php', {
'data': data
}, function (data) {
$('#variations').html(data);
});
});
HTML輸出
<div id="variations">
Array
(
[data] => Array
(
[0] => Array
(
[val] => 1
)
)
)
</div>
PHP
print_r($_POST)
問題:如果未選中復選框,如何從POST中刪除值?
使用$('form#myForm').serialize()
發送表單數據,它將僅send the checkboxes which are checked
//example 1
$('input#submitButton').click( function() {
$.post( 'some-url', $('form#myForm').serialize(), function(data) {
... do something with response from server
},
'json' // I expect a JSON response
);
});
//example 2
$('input#submitButton').click( function() {
$.ajax({
url: 'some-url',
type: 'post',
// dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
... do something with the data...
}
});
});
從這里
您可以使用.disable方法禁用對象,然后在發布完成后啟用它們。或者可以在單擊提交按鈕時使用方法.click函數,或者如果您不想在php中使用此值,則可以取消設置($ _POST ['一個特定鍵']),具有未設置的功能
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.