我遵循的是示例https://examples.bootstrap-table.com/index.html#options/table-ajax.html#view-source
<table
id="table"
data-toggle="table"
data-height="460"
data-ajax="ajaxRequest"
data-search="true"
data-side-pagination="server"
data-pagination="true">
</table>我需要在data-ajax="ajaxRequest"中传递一个参数,假设是switch = 1到下面的ajaxRequest脚本:
function ajaxRequest(params) {
$.ajax({
type: "post",
url: "process.php",
dataType: 'json',
data:{
//I want to pass *switch* variable here to process.php
//how?
}
success: function(items) {
params.success({
rows: items
}, null);
},
error: function(er) {
console.log(params.error(er))
console.log("error", er);
}
})
}请帮帮忙。
-编辑- 2021/09/29谢谢@Eitanmg,我读了那个文档。
如果我的问题不清楚,很抱歉。
所以,在view.php上:
<table
id="table"
data-toggle="table"
data-height="460"
data-ajax="ajaxRequest"
data-query-params="queryParams"
data-search="true"
data-side-pagination="server"
data-pagination="true">
</table>我希望将switch = 42传递给ajaxRequest,这样在稍后的ajaxRequest中,data:{ "switch": params.data.switch }将在process.php中处理,它包含:
if (switch == 42){
task1()
}else if (switch == 42){
task2()
}else { and so on}因此,"42“是从view.php动态发送的。而开关变量将根据该值在process.php上处理各种函数。
我正在寻找建议发送开关参数从php文件到ajaxRequest。
发布于 2021-08-03 12:32:43
您应该将data-query-params="queryParams"添加到您的表中,如下所示:
<table
id="table"
data-toggle="table"
data-height="460"
data-ajax="ajaxRequest"
data-query-params="queryParams"
data-search="true"
data-side-pagination="server"
data-pagination="true">
</table>并添加带有switch查询字符串参数的新函数
function queryParams(params) {
params.switch = 42
return params
}在你的ajaxRequest函数中,你应该这样访问它:
function ajaxRequest(params) {
$.ajax({
type: "post",
url: "process.php",
dataType: 'json',
data:{ "switch": params.data.switch },
success: function(items) {
params.success({
rows: items
}, null);
},
error: function(er) {
console.log(params.error(er))
console.log("error", er);
}
})
}发布于 2021-10-04 09:27:51
谢谢@eitanmg
我修改如下
在视图中,
<input type=hidden id=switch value=42> // <<<<--- add this
<table
id="table"
data-toggle="table"
data-height="460"
data-ajax="ajaxRequest"
data-query-params="queryParams"
data-search="true"
data-side-pagination="server"
data-pagination="true">
</table>并捕获变量
function queryParams(params) {
params.switch = document.getElementById(switch) // <<<----------catch like this
return params
}
function ajaxRequest(params) {
$.ajax({
type: "post",
url: "process.php",
dataType: 'json',
data:{ "switch": params.data.switch },
success: function(items) {
params.success({
rows: items
}, null);
},
error: function(er) {
console.log(params.error(er))
console.log("error", er);
}
})
}然后在process.php中处理它
if ($_POST('switch')==42) {}但是,如果你有任何比我上面编码的更聪明的方法,请欢迎。
https://stackoverflow.com/questions/68371688
复制相似问题