ホームページ  >  記事  >  バックエンド開発  >  jqueryは複数のチェックボックスの値を取得し、非同期でphpに送信します。

jqueryは複数のチェックボックスの値を取得し、非同期でphpに送信します。

WBOY
WBOYオリジナル
2016-08-08 09:22:28906ブラウズ

html code:

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

html のデータはデータベースから読み取られ、次のコードとして理解できます

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

jquery code:

<span> 1</span><span>var</span> mt4Ids =<span> [];
</span><span> 2</span> $('input[name=uid]').each(<span>function</span><span>() {
</span><span> 3</span><span>if</span>(<span>this</span><span>.checked) {
</span><span> 4</span>         mt4Ids.push($(<span>this</span><span>).val());
</span><span> 5</span><span>    }
</span><span> 6</span><span>});
</span><span> 7</span> data =<span> {
</span><span> 8</span><span>    mt4Ids : JSON.stringify(mt4Ids)
</span><span> 9</span><span>};
</span><span>10</span><span>var</span> pUrl = "/a/manageUser.html"<span>;
</span><span>11</span> $.post(pUrl, data, <span>function</span><span>(data){
</span><span>12</span><span>if</span>(data.state == 1<span>){
</span><span>13</span><span>        alert(data.msg);
</span><span>14</span>         location.href = "/h/permission.html"<span>;
</span><span>15</span>     }<span>else</span><span>{
</span><span>16</span>         alert("操作失败"<span>);
</span><span>17</span><span>    }
</span><span>18</span> }, 'json');

PHP code

<span> 1</span><span>$mt4Ids</span> = !<span>empty</span>(<span>$_POST</span>['mt4Ids']) ? <span>$_POST</span>['mt4Ids'] : <span>false</span><span>;
</span><span> 2</span><span> 3</span><span>$stripMt4Ids</span> = <span>preg_replace</span>('/[\"\[\]]/', '', <span>$mt4Ids</span><span>);
</span><span> 4</span><span>$mt4IdsToArr</span> = <span>explode</span>(',', <span>$stripMt4Ids</span><span>);
</span><span> 5</span><span> 6</span><span>foreach</span>(<span>$mt4IdsToArr</span><span>as</span><span>$uid</span><span>){
</span><span> 7</span>     permission_relation::add(<span>$uid</span>, <span>$gid</span><span>);
</span><span> 8</span><span>}
</span><span> 9</span><span>$data</span> = <span>array</span><span>(
</span><span>10</span>     'state' => 1,
<span>11</span>     'msg'   => '操作成功'
<span>12</span><span>);
</span><span>13</span><span>echo</span> json_encode(<span>$data</span><span>);
</span><span>14</span><span>return</span><span>false</span>;

// $ gid は無視できます

以上、複数のチェックボックスの値を取得してphpに非同期で送信するjqueryの紹介でした。内容も含めて、PHPチュートリアルに興味のある友人の参考になれば幸いです。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。