Php 如何在Laravel中使用ajax将两个值传递给控制器方法
我有两个选择框。首先,我选择框1值。然后我选择框2的值。当我选择select box 2时,我尝试使用ajax将select box 1和2的值传递给控制器方法。我只能传递一个值。如何传递两个值 选择框1Php 如何在Laravel中使用ajax将两个值传递给控制器方法,php,ajax,laravel,controller,return-value,Php,Ajax,Laravel,Controller,Return Value,我有两个选择框。首先,我选择框1值。然后我选择框2的值。当我选择select box 2时,我尝试使用ajax将select box 1和2的值传递给控制器方法。我只能传递一个值。如何传递两个值 选择框1 @foreach($routes as $route) <option name = 'origin' value='{{$route->rte_origin}}'>{{$route->rte_origin}}</o
@foreach($routes as $route)
<option name = 'origin' value='{{$route->rte_origin}}'>{{$route->rte_origin}}</option>
@endforeach
还有我的ajax代码:
<script type="text/javascript">
$(document).ready(function($){
$('#destination').change(function(){
var destination= $(this).find(':selected').text();
$.get("{{ URL::route('getDate')}}",
{destination: destination},
function(data) {
var model = $('#ja');
model.empty();
$.each(data, function(index, element) {
................
});
});
});
});
$(文档).ready(函数($){
$('#destination')。更改(函数(){
var destination=$(this.find(':selected').text();
$.get(“{{URL::route('getDate')}}”,
{目的地:目的地},
功能(数据){
var模型=$('ja');
model.empty();
$.each(数据、函数(索引、元素){
................
});
});
});
});
分别选择它们(按id):
这是您正在传递的数据:
{destination: destination},
您现在可以将其更改为传递更多项目:
{origin: origin, destination: destination},
{destination:destination}
周围的大括号表示它是一个数组,因此可以添加任意数量的值,并用逗号分隔。乙二醇
{key1:value1, key2:value3, key3:value3}
您可能还想知道,$.get函数只是完整ajax函数的快捷方式,它提供了更多功能,例如:
$.ajax({
url: "path/to.resource",
type: "GET", //can be any HTTP VERB, eg GET,POST,PUT,DELETE etc...
data:{data1:value1, data2:value2}, //any data you want to pass to the function
async: true, //make call synchronous or asynchronous
dataType:'json', //type of response you are expecting from server eg, text, json etc...
success: function (data) {
//code to run on successfully return. any returned data is contained in the data variable
},
error: function (xhr, ajaxOptions, thrownError) {
//code to run in the event of an error
},
});
关于jquery ajax函数使用的完整细节可以在这里找到:这不是一个完整的代码,这是您可以做的一个想法。例如,如果不在HTML选择中设置ID,它将不起作用。
{key1:value1, key2:value3, key3:value3}
$.ajax({
url: "path/to.resource",
type: "GET", //can be any HTTP VERB, eg GET,POST,PUT,DELETE etc...
data:{data1:value1, data2:value2}, //any data you want to pass to the function
async: true, //make call synchronous or asynchronous
dataType:'json', //type of response you are expecting from server eg, text, json etc...
success: function (data) {
//code to run on successfully return. any returned data is contained in the data variable
},
error: function (xhr, ajaxOptions, thrownError) {
//code to run in the event of an error
},
});