如何从视图文件调用控制器函数(从onchange属性的下拉列表)?

时间:2016-02-16 10:32:14

标签: php laravel-5

这是我的代码:

<select style="background-color: #ffffa0" name="country" onChange=""> 
            <option value="">---select country--</option> 
        <?php  
             $rows=DB::table('country')->get();
             foreach ($rows as $row)
             {
                   echo "<option value='$row->id'> $row->country </option>";    
             }         
         ?>
    </select>

如何从onChange调用控制器的功能?

1 个答案:

答案 0 :(得分:0)

 <html> <select style="background-color: #ffffa0" name="country" id="country"> 
            <option value="">---select country--</option> 
        <?php  
             $rows=DB::table('country')->get();
             foreach ($rows as $row)
             {
                   echo "<option value='$row->id'> $row->country </option>";    
             }         
         ?>
    </select>
    </html>

    <script>

    $(document).ready(function() {
$('#country').change(function(){

        var country=$('#country').val();

        $.ajax({
        type: "post",
        url: "AA/BB", // path to function
        cache: false,               
        data: { country: country},
        success: function(val){                     
        try{     
        }catch(e) {     
            alert('Exception while request..');
        }   

        },
        error: function(){                      
            alert('Error while request..');
        }

            });
        });
});
</script>