onselect checkbox add selected value to the URL as querystring

泄露秘密 提交于 2019-12-08 11:06:03

问题


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:


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

    });
});​


来源:https://stackoverflow.com/questions/9437952/onselect-checkbox-add-selected-value-to-the-url-as-querystring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!