首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bootstrap-table :将参数传递给$.ajax()

bootstrap-table :将参数传递给$.ajax()
EN

Stack Overflow用户
提问于 2021-07-14 11:34:41
回答 2查看 133关注 0票数 0

我遵循的是示例https://examples.bootstrap-table.com/index.html#options/table-ajax.html#view-source

代码语言:javascript
复制
<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脚本:

代码语言:javascript
复制
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上:

代码语言:javascript
复制
<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中处理,它包含:

代码语言:javascript
复制
if (switch == 42){ 
    task1()
 }else if (switch == 42){
    task2()
 }else { and so on}

因此,"42“是从view.php动态发送的。而开关变量将根据该值在process.php上处理各种函数。

我正在寻找建议发送开关参数从php文件到ajaxRequest

EN

回答 2

Stack Overflow用户

发布于 2021-08-03 12:32:43

您应该将data-query-params="queryParams"添加到您的表中,如下所示:

代码语言:javascript
复制
<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查询字符串参数的新函数

代码语言:javascript
复制
  function queryParams(params) {
    params.switch = 42
    return params
  }

在你的ajaxRequest函数中,你应该这样访问它:

代码语言:javascript
复制
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);
            }
        })
    }
票数 0
EN

Stack Overflow用户

发布于 2021-10-04 09:27:51

谢谢@eitanmg

我修改如下

在视图中,

代码语言:javascript
复制
<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>

并捕获变量

代码语言:javascript
复制
  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中处理它

代码语言:javascript
复制
if ($_POST('switch')==42) {}

但是,如果你有任何比我上面编码的更聪明的方法,请欢迎。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68371688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档