本文介绍了onselect checkbox将选中的值添加到URL作为querystring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网页加载网址为(医院)。
现在在页面加载后,如果有人选择Offices复选框,它应该将该Checkbox'Office'的值附加到URL
(医院或办公室)

The page loads with the URL as http://mysite.aspx/result.aspx?k=("Hospital").Now after the page load, if someone selects Offices checkbox, it should append the value of that Checkbox 'Office' to the URL ashttp://mysite.aspx/result.aspx?k=("Hospital" OR "Office")

我如何在jquery中这样做?

How do I do this in jquery?

<div class="LocationSearchBox">
  <input name="KeywordBox" class="BasicSearchInputBox" type="text" value="Type a Keyword.."/>
  <div class="searchBtnHolder"><a class="searchButton" href="#" type="submit"><span>Search</span></a></div>
</div>
<br/><br/>
<div class="MyOptions">
    Hospitals<input name="LocType" type="checkbox" value="Hospital"/> &#160;
    Offices<input name="LocType" type="checkbox" value="Office"/> &#160;
    Emergency Centers<input name="LocType" type="checkbox" value="Emergency"/>&#160;
    Out-Patient Centers<input name="LocType" type="checkbox" value="Out-Patient"/>&#160;
    Facilities<input name="LocType" type="checkbox" value="Facility"/>
</div>


<script type="text/javascript" language="javascript">
 $(document).ready(function() {
    var url = 'http://mysite.com/results.aspx?k=("Hospital");
    $(".LocationSearchBox a.searchButton").click(function(){
        var chkboxVal = $("input[name='LocType']:checked").val();
        var keywords = encodeURIComponent($(".BasicSearchInputBox").val());
            url =url+"?kwd="+keywords+"&type="+chkboxVal;
            window.location.href=url;
        });
    }):

 });


推荐答案

您的代码应该是这样的...

Your code should have been something like this ...

$(document).ready(function() {

var url = 'http://mysite.com/results.aspx';

$(".MyOptions input").click(function() {

    var urlValues = window.location.href.split("k=(")[1];
    urlValues = urlValues.substring(0,urlValues .length - 1);

    var checkboxValues = $("input[name=LocType]:checked").map(function() {return "\"" + this.value + "\"";}).get().join(" OR ")

    if (urlValues.length > 0)
        urlValues += " OR " + checkboxValues;

    var keywords = encodeURIComponent($(".BasicSearchInputBox").val());
    window.location.href = "http://mysite.aspx/result.aspx?k=(" + urlValues + ")";

    });
});​

这篇关于onselect checkbox将选中的值添加到URL作为querystring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 03:52