Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果使用jquery/javascript将url设置为特定值,如何隐藏和显示div?_Javascript_Php_Jquery_Html - Fatal编程技术网

如果使用jquery/javascript将url设置为特定值,如何隐藏和显示div?

如果使用jquery/javascript将url设置为特定值,如何隐藏和显示div?,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我通过url发送一个特定的值,然后我的页面会得到刷新。 URL值是动态的。每当设置URL时,我都希望显示已隐藏的div <button id="show_id" onclick="location.href='opdcasepaperreport.php?patient_nm='+document.getElementById('patient_id').value;" > View Report </button> })) 及 在第一种情况下,由于页面刷新div,

我通过url发送一个特定的值,然后我的页面会得到刷新。 URL值是动态的。每当设置URL时,我都希望显示已隐藏的div

<button  id="show_id"  onclick="location.href='opdcasepaperreport.php?patient_nm='+document.getElementById('patient_id').value;" > View Report </button>
}))

在第一种情况下,由于页面刷新div,将再次可见和不可见。 如果(isset(---){//dothis;}

这样做如何:

$("#show_id").click(function(e)
{
  e.preventDefault();//this will prevent page refresh
  $('#main').toggle();
});

更改location.href值仍将刷新页面

要根据url值隐藏/显示div,您需要:

  • 通过在url参数中搜索获取值
  • 显示/隐藏分区
  • 获取URL参数。

    您可以使用此脚本获取url参数:

     var getQuery = function () {
      var url_params = {};
      var query = window.location.search.substring(1);
      if (query.length === 0) return false;
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (typeof url_params[pair[0]] === "undefined") {
          url_params[pair[0]] = pair[1];
        } else if (typeof url_params[pair[0]] === "string") {
          var arr = [ url_params[pair[0]], pair[1] ];
          url_params[pair[0]] = arr;
        } else {
          url_params[pair[0]].push(pair[1]);
        }
      } 
     return url_params;
    };
    
    或者通过使用.show()/.hide()来

    因此:

    但最好不要使用url参数来更改视图

    玩得开心。

    尝试以下代码:

     $( document ).ready(function()
        {
        function getParameterByName(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
    
            var hasParam = getParameterByName('patient_nm');
            if(hasParam !== 'undefined') {
             $('#main').toggle();
            }
        });
    
    根据您的代码,单击按钮时,元素将被切换,但之后页面将立即刷新。在页面刷新时,元素将再次隐藏,因此您无法查看效果

    根据我上面的代码,当页面刷新/加载时,它将搜索您正在URL中添加的参数“patient_nm”。如果此参数不为空,它将切换元素。由于此过程发生在页面加载之后,因此您将能够看到结果


    希望这能有所帮助。

    单击后,您想在主页上隐藏/显示div还是在重定向页面上隐藏/显示div?使用该按钮可以做两件事。使用jquery函数生成div hide/show,并且还应用了onclick将页面重定向到特定的url。这是故意的吗?如果保留此实现,页面将由于稍后的功能(onclick)而刷新/重定向
     var getQuery = function () {
      var url_params = {};
      var query = window.location.search.substring(1);
      if (query.length === 0) return false;
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (typeof url_params[pair[0]] === "undefined") {
          url_params[pair[0]] = pair[1];
        } else if (typeof url_params[pair[0]] === "string") {
          var arr = [ url_params[pair[0]], pair[1] ];
          url_params[pair[0]] = arr;
        } else {
          url_params[pair[0]].push(pair[1]);
        }
      } 
     return url_params;
    };
    
    $("#your_div_name").toggle();
    
    var query = getQuery();
    if (query && query.some_param !== undefined) {
        $("#your_div_name").show()
    } else {
        $("#your_div_name").hide()
    }
    
     $( document ).ready(function()
        {
        function getParameterByName(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
    
            var hasParam = getParameterByName('patient_nm');
            if(hasParam !== 'undefined') {
             $('#main').toggle();
            }
        });