【问题标题】:add querystring to URL on page load在页面加载时将查询字符串添加到 URL
【发布时间】:2012-02-24 20:37:36
【问题描述】:

原始网址是http://mysite.com/locations.aspx 当有人单击某处的位置链接并进入此页面时,在页面加载时,我希望将 URl 即时附加如下?医院是页面上的一个复选框,默认需要选中。因此,当他们登陆页面时,他们会看到所有医院,而不必检查医院复选框并重新加载页面。这可以使用 jquery 吗?

http://mysite.com/locations.aspx?k=("医院")

Find locations by&#160;<input name="Keyword" type="text" size="70"/><input name="Search" type="submit" value="Search"/><br/><br/>

Hospitals<input name="Hospitals" type="checkbox" value="Hospital"/> &#160; 
Offices<input name="Office" type="checkbox" value="Office"/> &#160; 
Emergency Centers<input name="EmergencyCenters" type="checkbox" value="Emergency"/>&#160; 
Out-Patient Centers<input name="Out-Patient" type="checkbox" value="Out-Patient"/>&#160; 
Facilities<input name="Facility" type="checkbox" value="Facility"/>

【问题讨论】:

标签: jquery url checkbox query-string checked


【解决方案1】:

您可以(在页面 http://mysite.com/locations.aspx 内)创建如下函数:

function getDefault() {
var trail = '?k=("Hospital")';
var url = "http://mysite.com/locations.aspx" + trail;
window.location = url;
}

并将onload 添加到&lt;body&gt; 标签中

<body onload="getDefault();">

当然,它会重新加载页面(无需用户干预),但会使用新的尾随参数。

...至于复选框,您可以添加适当的属性

$(document).ready(function() {
$("input[name=Hospitals]").attr('checked','checked');
}); //  ready

它会被即时检查。

【讨论】:

    【解决方案2】:

    在较新的浏览器中,您可以使用HTML5 history api 更改网址而无需重新加载页面,但是,不支持它的旧浏览器将需要重新加载页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2018-07-14
      相关资源
      最近更新 更多