shikhar17 says:
Hello everyone, This is regarding this post :- http://www.aspsnippets.com/Articles/Send-Pass-Data-Values-from-one-page-to-another-using-jQuery.aspx where i found way to pass data from one page to another. I took the query string method and everything is working fine but how shall i pass whether a check box is in form is checked or not ? even if its checked i want to pass the desired value like { <input type="checkbox" id="newbox"> This is a checkbox }
So, If the check box is checked i want to pass the text written beside it and if it is unchecked i want nothing there on second page. Please help.
Hi shikhar17,
You just need to check checkbox is checked or not on btnQueryString click event by checking is(':checked'). if its checked then append the query string value to url string value.
And on destination page same way you just need to check if query string is not null then show the value of checked checkbox. Source Page
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
Name:
<input type="text" id="txtName" name="Name" value="Mudassar Khan" /><br />
<br />
Technology:
<select id="ddlTechnolgy" name="Technology">
<option value="ASP.Net">ASP.Net</option>
<option value="PHP">PHP</option>
<option value="JSP">JSP</option>
</select>
<br />
<br />
<input type="checkbox" id="newbox" value="Test Value"/> newbox
<br />
<input type="button" id="btnQueryString" value="Send" />
<script type="text/javascript">
$(function () {
$("#btnQueryString").bind("click", function () {
var url = "DestinationPage.aspx?name=" + encodeURIComponent($("#txtName").val()) + "&technology=" + encodeURIComponent($("#ddlTechnolgy").val());
if ($("#newbox").is(':checked')) {
url = url + "&newbox=" + encodeURIComponent($("#newbox").val());
}
window.location.href = url;
});
});
</script>
DestinationPage
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<span id="lblData"></span>
<br />
<br />
</div>
<script type="text/javascript">
var queryString = new Array();
$(function () {
if (queryString.length == 0) {
if (window.location.search.split('?').length > 1) {
var params = window.location.search.split('?')[1].split('&');
for (var i = 0; i < params.length; i++) {
var key = params[i].split('=')[0];
var value = decodeURIComponent(params[i].split('=')[1]);
queryString[key] = value;
}
}
}
if (queryString["name"] != null && queryString["technology"] != null) {
var data = "<u>Values from QueryString</u><br /><br />";
data += "<b>Name:</b> " + queryString["name"] + " <b>Technology:</b> " + queryString["technology"];
if (queryString["newbox"] != null) {
data += " <b>newbox:</b> " + queryString["newbox"];
}
$("#lblData").html(data);
}
});
</script>
Screenshot
