溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

jquery怎么獲取多個checkbox的值異步提交給php

發(fā)布時間:2021-08-03 17:44:09 來源:億速云 閱讀:134 作者:chen 欄目:開發(fā)技術

這篇文章主要介紹“jquery怎么獲取多個checkbox的值異步提交給php”,在日常操作中,相信很多人在jquery怎么獲取多個checkbox的值異步提交給php問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”jquery怎么獲取多個checkbox的值異步提交給php”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

本文實例講述了jquery獲取多個checkbox的值異步提交給php的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

html代碼:

<tr>
  <td><input type="checkbox" name="uid" value="<?=$item['mtaccount_id']?>"></td>
  <td><?=$item['mtaccount_id']?></td>
  <td><?=$item['account_id']?></td>
  <td><?=$item['account_name']?></td>
  <td><?=$item['server']?></td>
  <td><?=$item['platform']?></td>
</tr>

我的是html里的數(shù)據(jù)是從數(shù)據(jù)庫讀出來的,在此可以理解為下面代碼

<li><input type="checkbox" name="uid" value="1" />用戶1</li>
<li><input type="checkbox" name="uid" value="2" />用戶2</li>
<li><input type="checkbox" name="uid" value="3" />用戶3</li>
<li><input type="checkbox" name="uid" value="4" />用戶4</li>

jquery代碼:

var mt4Ids = [];
 $('input[name=uid]').each(function() {
   if(this.checked) {
     mt4Ids.push($(this).val());
   }
 });
 data = {
   mt4Ids : JSON.stringify(mt4Ids)
 };
var pUrl = "/a/manageUser.html";
$.post(pUrl, data, function(data){
   if(data.state == 1){
     alert(data.msg);
     location.href = "/h/permission.html";
   }else{
     alert("操作失敗");
   }
 }, 'json');

PHP代碼

$mt4Ids = !empty($_POST['mt4Ids']) ? $_POST['mt4Ids'] : false;
$stripMt4Ids = preg_replace('/[\"\[\]]/', '', $mt4Ids);
$mt4IdsToArr = explode(',', $stripMt4Ids);
foreach($mt4IdsToArr as $uid){
   permission_relation::add($uid, $gid);
}
$data = array(
   'state' => 1,
   'msg'  => '操作成功'
);
echo json_encode($data);
return false;
// $gid 可忽略

到此,關于“jquery怎么獲取多個checkbox的值異步提交給php”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI