使用laravel 5使用<select onchange()=“”> <option>更新数据库中的数据

时间:2016-02-09 06:41:23

标签: php ajax laravel-5.1

请帮我。我如何使用select选项更新数据库,ajax onchange而不提交。 我的看法 : {! Form :: model($ UserAccess,[&#39; method&#39; =&gt;&#39; POST&#39;,&#39; url&#39; =&gt; [&#39; setting / updaterole&#39 ;]])!!}                     &lt; table class =&#34; dataTable&#34; ID =&#34;表用户&#34;&GT;                         &lt; thead class =&#34; grey lighten-3&#34;&gt;                             &LT; TR&GT;                                 &lt; td class =&#34; center-align no-sort&#34;&gt; Photo&lt; / td&gt;                                 &lt; td class =&#34; center-align&#34;&gt; Fullname&lt; / td&gt;                                 &lt; td class =&#34; center-align&#34;&gt;规则&lt; / td&gt;                                 &lt; td class =&#34; center-align&#34;&gt;操作&lt; / td&gt;                             &LT; / TR&GT;                         &LT; / THEAD&GT;                         &LT; TBODY&GT;                             &lt;?php $ hitung = $ UserAccess-&gt; count(); ?&GT;                             @foreach($ UserAccess as $ list)                             &LT; TR&GT;                                 &lt; td class =&#34; center-align&#34;宽度=&#34; 50&#34;&GT;                                     @if($ list-&gt; avatar == NULL)                                     &lt; img class =&#34; circle responsive-img&#34;宽度=&#34; 50&#34; SRC =&#34; {{资产(配置(&#39; param.url_uploads&#39;)&#39; blank.jpg&#39;)}}&#34; /&GT;                                     @其他                                     &lt; img class =&#34; circle responsive-img&#34;宽度=&#34; 50&#34; SRC =&#34; {{$列表 - &GT;化身}}&#34; /&GT;                                     @万一                                 &LT; / TD&GT;                                 &lt; td class =&#34; red1-text lato-bold center-align&#34;&gt; {{$ list-&gt; name}}&lt; / td&gt;                                 &lt; td class =&#34; center-align&#34;宽度=&#34; 150&#34;&GT;                                     &lt; select id =&#34; selectrole&#34;名称=&#34; selectrole&#34;数据-URL =&#34; {{URL(&#39; /设置/ updaterole&#39;)}}&#34; data-token =&#34; {{csrf_token()}}&#34;&gt;                                         @foreach($ UserAccessRole as $ listRole)                                             &lt; option value =&#34; {{$ listRole-&gt; id}}&#34; @if($ list-&gt; user_access_role_id_fk == $ listRole-&gt; id)selected =&#34; selected&#34; @ endif&gt; {{$ listRole-&gt; name}}&lt; / option&gt;                                         @endforeach                                     &LT; /选择&GT;                                 &LT; / TD&GT;                                 &lt; input type =&#34; hidden&#34; value =&#34; {{$ list-&gt; id}}&#34; name =&#34; idmain&#34;&gt;                                 @if($ hitung&gt; 2)                                     &lt; td class =&#34; center-align&#34;&gt;                                         &lt; a href =&#34; remove_access / {{$ list-&gt; id}} / delete&#34; &gt;删除访问权限&lt; a /&gt;                                     &LT; / TD&GT;                                 @其他                                     &lt; td class =&#34; center-align&#34;&gt;删除访问权限&lt; / td&gt;                                 @万一                             &LT; / TR&GT;                             @endforeach                         &LT; / tbody的&GT;                     &LT; /表&gt; {! Form :: close()!!} @section(&#39;页面脚本&#39;) &lt; script type =&#34; text / javascript&#34;&gt; $(&#39;选择&#39)。在(&#39;变更&#39;,函数(){ var city = $(&#34; select option:selected&#34;).val(); var token = $(this).data(&#39; token&#39;); var base_url = $(this).data(&#39; url&#39;);  $就({     网址:BASE_URL +&#39; / updaterole&#39 ;,     键入:&#39; POST&#39;,     data:{_token:token,city_id:city_id},     成功:函数(MSG){        警报(&#34;成功&#34);     }  }); }) &LT; /脚本&GT; @endsection 我的控制器: 公共函数doUpdateAccessRole($ id,Request $ request) {    $ place_id = $ request-&gt; get(&#39; id&#39;);    $ Role = UserAccess :: findOrFail($ id);    $ Role-&gt; user_access_role_id_fk = $ id_role;    $基于角色&GT;更新($请求 - &GT;所有());    回归成功&#39 ;; } 我的路线 路线::交(&#39;设置/ updaterole&#39; [&#39;使用&#39; =&GT;&#39; SettingController @ doUpdateAccessRole&#39;&#39;如&#39; =&GT; &#39; updateaccessrole&#39;]); 我不明白我的代码有什么问题。当我更改选择选项值时,我的程序没有任何反应。请帮帮我,谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个

$('#selectrole').on('change',function(){
var city =  $(this).val();
var token = $(this).data('token');
var base_url = $(this).data('url');
$.ajax({
    url:base_url+'/updaterole',
    type: 'POST',
    data: { _token :token,city_id:city_id },
    success:function(msg){
        alert("success");
    }
});

})