在使用AJAX提交表单后,我对保持分区处于活动状态有问题.我的场景是我有一个搜索栏,它可以接受多种类型的输入(名称、ID、类别等).然后,我有一个过滤器栏,这基本上是一个高级搜索,它实际上做同样的事情,但有更多的选项,如名称,ID,类别,价格范围等单独的字段,这些是一些重要的术语,

  1. ID:产品ID
  2. 名称:产品名称
  3. SPL_CD:分配给每个产品的特殊代码
  4. Alt_NM2:备用名称/描述
  5. 成本开始/成本结束:范围搜索成本价在该范围内的所有产品
  6. SellStart/sellEnd:范围搜索售价在范围内的所有产品
  7. Cname:产品类别
  8. GNAME:组产品被归入类别

我不太关心css,but the point is that, anyone using this page at client end needs to see the values inserted in the filter dropdown i.e. keeping the filter drop down visible even on page reload.提交表单后,我的整个页面都会重新加载.这会使下拉菜单再次隐藏.我不擅长阿贾克斯,所以我不知道怎么做,任何帮助都将不胜感激.

这是有用的工具(忽略缺少css,因为它主要是在基于MVC方法的不同文件下定义的)-Fiddle 这种操作也不支持在表单提交后保持分区活动,所以我try 切换到AJAX.

这就是我的AJAX方法,但它一直显示空白,而不是显示表及其数据.

$("#searchButton").click(function() {
        var params = {
            searchKeyword: $("#searchKeyword").val()
        };
        var form = $("#searchArea");
        var url = form.attr("action")+"?"+$.param(params);
        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            colModel:[
                {label:'ProductID', name:'ITM_CD',classes:'ITM_CD', width:40, align:"center", sorttype:"int"},
                {label:'Name', name:'ITM_NM',classes:'ITM_NM',width:100, align:"center", sorttype:"int"},
                {label:'Alt.name', name:'ITM_ALT_NM2',classes:'ITM_ALT_NM2', width:100, align:"center", sorttype:"string"},
                {label:'Special code', name:'SPL_CD',classes:'SPL_CD', width:50, align:"center", sorttype:"int"},
                {label:'Sort Order', name:'SORDER',classes:'SORDER', width:40, align:"center", sorttype:"int"},
                {label:'Talent Points', name:'TPOINTS',classes:'TPOINTS', width:40, align:"center", sorttype:"int"},
                {label:'Selling price', name:'SPRICE',classes:'SPRICE', width:30, align:"right", sorttype:"string"},
                {label:'Cost price', name:'CPRICE',classes:'CPRICE', width:30, align:"right", sorttype:"string"},
                {label: 'Category Name', name:'CNAME', classes: 'CNAME', width: 30, align: "right", sorttype:"string"},
                {label: 'Group name', name:'GNAME', classes: 'GNAME', width: 30, align: "right", sorttype:"string"}
        ],
        });
    });
    $("#searchButton").click(function() {
        var params = {
          searchKeyword: $("#searchKeyword").val(),
            IDKeyword: $("#IDKeyword").val(),
            NMKeyword: $("#NMKeyword").val(),
            NM2Keyword: $("#NM2Keyword").val(),
            SCKeyword: $("#SCKeyword").val(),
            costStartKeyword: $("#costStartKeyword").val(),
            costEndKeyword: $("#costEndKeyword").val(),
            sellStartKeyword: $("#sellStartKeyword").val(),
            sellEndKeyword: $("#sellEndKeyword").val(),
            cnameKeyword: $("#cnameKeyword").val(),
            gnameKeyword: $("#gnameKeyword").val()
        };
        var form = $("#dropDownFilter");
        var url = form.attr("action")+"?"+$.param(params);
        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            colModel:[
                {label:'ProductID', name:'ITM_CD',classes:'ITM_CD', width:40, align:"center", sorttype:"int"},
                {label:'Name', name:'ITM_NM',classes:'ITM_NM',width:100, align:"center", sorttype:"int"},
                {label:'Alt.name', name:'ITM_ALT_NM2',classes:'ITM_ALT_NM2', width:100, align:"center", sorttype:"string"},
                {label:'Special code', name:'SPL_CD',classes:'SPL_CD', width:50, align:"center", sorttype:"int"},
                {label:'Sort Order', name:'SORDER',classes:'SORDER', width:40, align:"center", sorttype:"int"},
                {label:'Talent Points', name:'TPOINTS',classes:'TPOINTS', width:40, align:"center", sorttype:"int"},
                {label:'Selling price', name:'SPRICE',classes:'SPRICE', width:30, align:"right", sorttype:"string"},
                {label:'Cost price', name:'CPRICE',classes:'CPRICE', width:30, align:"right", sorttype:"string"},
                {label: 'Category Name', name:'CNAME', classes: 'CNAME', width: 30, align: "right", sorttype:"string"},
                {label: 'Group name', name:'GNAME', classes: 'GNAME', width: 30, align: "right", sorttype:"string"}
        ],
        });
    });
});

Html和css与提琴是一样的.

编辑1:try 了本地存储

$("#filterSearchButton").on("click", function(){
        localStorage.setItem("style", $("#dropDownFilter").css("display", "none"));
    });
    if (localStorage.getItem("style") === null) {
        localStorage.setItem("style", $("#dropDownFilter").css("display", "flex"));
    } else {
        $("#dropDownFilter").css("display", "flex");
    }
});

Problem: The dropdown displays on first reload too, need to avoid that.

推荐答案

您可以使用本地存储setItemgetItem来存储AJAX上次响应.

window.localStorage.setItem("variable", variable);

var getItem = window.localStorage.getItem("variable");

此外,当您想要将setItem设置为remove时,您也可以将其删除.

window.localStorage.removeItem('variable');

Javascript相关问答推荐

使用JavaScript单击上一个或下一个特定按钮创建卡滑动器以滑动单个卡

为什么从liveWire info js代码传递数组我出现错误?

Klaro与Angular的集成

如何避免移动设备中出现虚假调整大小事件?

我不知道为什么setwritten包装promise 不能像我预期的那样工作

MongoDB中的引用

WebGL 2.0无符号整数输入变量

使用LocaleCompare()进行排序时,首先使用大写字母

在react JS中映射数组对象的嵌套数据

虚拟滚动实现使向下滚动可滚动到末尾

Websocket错误—有一个或多个保留位开启:reserved1 = 1,reserved2 = 0,reserved3 = 0

Ember.js 5.4更新会话存储时如何更新组件变量

您能在卸载程序(QtInsteller框架)上添加WizardPage吗?

NG/Express API路由处理程序停止工作

使用auth.js保护API路由的Next.JS,FETCH()不起作用

为什么当我更新数据库时,我的所有组件都重新呈现?

在不扭曲纹理的情况下在顶点着色器中旋转UV

P5.js中矩形内的圆弧

为什么我的Navbar.css没有显示在本地主机页面上?

将promise列表拆分到组.按组并行和顺序执行所有promise