Heim > Backend-Entwicklung > PHP-Tutorial > jquery ruft die Werte mehrerer Kontrollkästchen ab und übermittelt sie asynchron an PHP

jquery ruft die Werte mehrerer Kontrollkästchen ab und übermittelt sie asynchron an PHP

WBOY
Freigeben: 2016-08-08 09:22:28
Original
929 Leute haben es durchsucht

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>
Nach dem Login kopieren

Meine Daten in HTML werden aus der Datenbank gelesen, was als folgender Code verstanden werden kann

<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>
Nach dem Login kopieren

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');
Nach dem Login kopieren

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>;
Nach dem Login kopieren

// $gid kann ignoriert werden

Das Obige stellt jquery vor, um mehrere Kontrollkästchenwerte abzurufen und sie asynchron an PHP zu senden, einschließlich des Inhalts. Ich hoffe, es wird für Freunde hilfreich sein, die sich für PHP-Tutorials interessieren.

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage