Maison  >  Article  >  développement back-end  >  jquery获取多个checkbox的值异步提交给php

jquery获取多个checkbox的值异步提交给php

WBOY
WBOYoriginal
2016-08-08 09:22:28903parcourir

html代码:

<span><span>tr</span><span>></span><span><span>td</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>td</span><span>></span><span></span><span>=$item['mtaccount_id']</span><span>?></span><span></span><span>td</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>td</span><span>></span><span></span><span>=$item['account_name']</span><span>?></span><span></span><span>td</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>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></span></span></span></span></span></span></span></span>

我的是html里的数据是从数据库读出来的,在此可以理解为下面代码

<span><span>li</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>li</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>li</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>li</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></span></span></span></span></span></span></span></span>

jquery代码:

<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代码

<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 可忽略

以上就介绍了jquery获取多个checkbox的值异步提交给php,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:Nginx缓存整理Article suivant:php将数据写入excel