假設我有一個非常相似的代碼
<form>
<input type="checkbox" name="account1" value="1">
<input type="checkbox" name="account2" value="2">
<input type="checkbox" name="account3" value="3">
<input type="checkbox" name="account4" value="4">
<button type="submit">Delete</button>
</form>
現在,一旦用戶選中復選框而不單擊提交/洗掉按鈕,我想將復選框的值存盤在 php 會話中。我打算為此使用 jquery/ajax,但我仍然無法完全理解它,因為我對 ajax 或 jquery 非常陌生。
要添加更多詳細資訊,程序是這樣的.. 用戶可以選擇他想要檢查的任何帳戶。假設他在第 1 頁檢查了一個帳戶。然后他轉到另一個頁面(分頁),然后回傳到第 1 頁,選中的框仍會被選中。
uj5u.com熱心網友回復:
您可以onchange為此使用事件偵聽器。
活生生的例子在這里。
索引.php
<form>
<input type="checkbox" name="account1" value="1">
<input type="checkbox" name="account2" value="2">
<input type="checkbox" name="account3" value="3">
<input type="checkbox" name="account4" value="4">
<button type="submit">Delete</button>
</form>
<br/><strong>You can see the checkbox values in session below...</strong>
<div id="newDiv">
</div>
<script>
$('*[type="checkbox"]').on("change", function(){
var check = 0;
if(this.checked == true) check = 1;
var value = this.value;
$.post("session.php", { box : value, check : check }, function(data, status){
if(status == "success"){
$('#newDiv').load("values.php");
}
})
})
</script>
會話檔案
<?
session_start();
if(isset($_POST['box'])){
//Separate session variable for each checkbox.
$checkbox = "checkBox".$_POST['box'];
$_SESSION[$checkbox] = $_POST['check'];
}
?>
值.php
<?
session_start();
print_r($_SESSION);
?>
如有任何疑問,請評論。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/378978.html
標籤:javascript php html 查询 阿贾克斯
