admin管理员组文章数量:1389855
I have multiple checkboxes. I have stored the checkbox values to the local storage, enabling the user to refresh the page and still have the checked or unchecked checkboxes displayed. Now I need to save the check box value as a url parameter, so that the page can be sent as a link retaining all the unchecked or checked values. Code below:
HTML: (check boxes)
<input type="checkbox" class="faChkRnd" name="likebox" id="like">
<input type="checkbox" class="faChkRnd" name="likebox" id="like1">
<input type="checkbox" class="faChkRnd" name="likebox" id="like2">
<input type="checkbox" class="faChkRnd" name="likebox" id="like2">
Update: please note I'm using a tabbed navigation menu and have added tab hash to url. Therefore a url will already look something like: www.example/index.html#home
I have multiple checkboxes. I have stored the checkbox values to the local storage, enabling the user to refresh the page and still have the checked or unchecked checkboxes displayed. Now I need to save the check box value as a url parameter, so that the page can be sent as a link retaining all the unchecked or checked values. Code below:
HTML: (check boxes)
<input type="checkbox" class="faChkRnd" name="likebox" id="like">
<input type="checkbox" class="faChkRnd" name="likebox" id="like1">
<input type="checkbox" class="faChkRnd" name="likebox" id="like2">
<input type="checkbox" class="faChkRnd" name="likebox" id="like2">
Update: please note I'm using a tabbed navigation menu and have added tab hash to url. Therefore a url will already look something like: www.example./index.html#home
Share edited Jul 27, 2020 at 12:17 Mosh Feu 29.4k18 gold badges93 silver badges141 bronze badges asked Jan 19, 2016 at 13:50 About LerosAbout Leros 1905 silver badges16 bronze badges 3- Please post your code what you already have tried! – Tewdyn Commented Jan 19, 2016 at 13:51
- You may be looking for $.param(). – Frédéric Hamidi Commented Jan 19, 2016 at 13:54
- stackoverflow./a/34878885/4763793 – Rino Raj Commented Jan 20, 2016 at 3:50
3 Answers
Reset to default 4You can use .serialize() but you have to change the name
atrribute's values.
$('button').click(function(){
alert($('input').serialize());
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="faChkRnd" name="likebox1" id="like">
<input type="checkbox" class="faChkRnd" name="likebox2" id="like1">
<input type="checkbox" class="faChkRnd" name="likebox3" id="like2">
<input type="checkbox" class="faChkRnd" name="likebox4" id="like2">
<button>getUrl</button>
The next step is to get the values from the QueryString and set the checkboxes
.
It's should looks like this:
$.each(location.search.replace('?', '').split('&'), function(i, seg) {
$('[name=' + seg.split('=')[0] + ']').attr('checked', 'checked');
});
http://jsbin./hecora/edit?html,js
Update
You can do "auto saving" when the user check/uncheck the checkboxes using hash
or do pushState
and add the params to the url (That's good in case from some reason you can't use the hash
for this). Of course you need to check the browsers support first.
Example (of pushState
):
var checkboxes = $('input[type="checkbox"]');
checkboxes.change(function() {
var ser = '?' + checkboxes.serialize();
history.pushState(null, null, ser);
});
$.each(location.search.replace('?', '').split('&'), function(i, seg) {
$('[name=' + seg.split('=')[0] + ']').attr('checked', 'checked');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="faChkRnd" name="likebox1" id="like">
<input type="checkbox" class="faChkRnd" name="likebox2" id="like1">
<input type="checkbox" class="faChkRnd" name="likebox3" id="like2">
<input type="checkbox" class="faChkRnd" name="likebox4" id="like2">
http://jsbin./nofoto/edit?html,js
Using Jquery, based on this answer:
var checkArray = new Array();
$('input[type=checkbox]').each(function () {
if (this.checked) checkArray.push(this.id)
});
var urlParameter = checkArray.join('&')
This would generate a string, containing the ids of all checked checkboxes. It works by iterating over all checkboxes on the page, saving the ids and then concatenating them with '&'. On loading the page you only have read the url parameters (e.g. http://website.?checkbox1&checkbox2 would check checkbox1 and checkbox2) and check the boxes like you did with the local storage.
$('input[type=checkbox]')
should use
$("input[type='checkbox'][name='likebox']").each( ...
本文标签: How to store check box values using javascript or jqueryStack Overflow
版权声明:本文标题:How to store check box values using javascript or jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744652359a2617759.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论