onselect checkbox add selected value to the URL as

2019-06-13 20:34发布

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 as http://mysite.aspx/result.aspx?k=("Hospital" OR "Office")

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;
        }); 
    }):

 });

1条回答
Fickle 薄情
2楼-- · 2019-06-13 20:51

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 + ")";

    });
});​
查看更多
登录 后发表回答