admin管理员组文章数量:1326099
I have a select box. On page load, "5" is selected, when I select "1" from the list, I want to reload the page and have 1 be selected on default. How do I acplish this in JQuery.
<span>
There are
<select class="num_apples form-control">
<option value="1">1</option>
<option value="5" selected="selected">5
</option><option value="50">50</option>
</select>
apples on the table
</span>
I tried doing this in JQuery
$(".num_apples").change (function() {
$(this).val = $(".num_apples").selected;
location.reload();
});
I have a select box. On page load, "5" is selected, when I select "1" from the list, I want to reload the page and have 1 be selected on default. How do I acplish this in JQuery.
<span>
There are
<select class="num_apples form-control">
<option value="1">1</option>
<option value="5" selected="selected">5
</option><option value="50">50</option>
</select>
apples on the table
</span>
I tried doing this in JQuery
$(".num_apples").change (function() {
$(this).val = $(".num_apples").selected;
location.reload();
});
Share
Improve this question
asked Sep 2, 2016 at 18:00
user6758349user6758349
4
-
localStorage
possibly – depperm Commented Sep 2, 2016 at 18:02 - 1 can add drop down value in query string/cookie and on window.load check if value is set or not and take appropriate action – Taimur Khan Commented Sep 2, 2016 at 18:04
- By the way why you want to reload the page on drop down change? are you using any server side technology? – Taimur Khan Commented Sep 2, 2016 at 18:06
- @TaimurKhan no, this is all done in the front end – user6758349 Commented Sep 2, 2016 at 18:08
2 Answers
Reset to default 4Save the selected value and check on page load if a value is saved and set the value of the select
accordingly:
var selected = localStorage.getItem('selected');
if (selected) {
$(".num_apples").val(selected);
}
$(".num_apples").change(function() {
localStorage.setItem('selected', $(this).val());
location.reload();
});
Example
You can set the selected value in local storage.
<span>
There are
<select id="mySelect" class="num_apples form-control">
<option value="1">1</option>
<option value="5" selected="selected">5</option>
<option value="50">50</option>
</select>
apples on the table
</span>
The JavaScript code :
<script>
$(document).ready(function(){
var mySelectBoxVal = localStorage.getItem("mySelectBoxVal");
if (mySelectBoxVal !== '') {
$('#mySelect').val(mySelectBoxVal);
}
$("#mySelect").on('change',function() {
selectBoxVal = $('#mySelect').val();
if (typeof(Storage) !== "undefined") {
localStorage.setItem("mySelectBoxVal", selectBoxVal);
} else {
alert('Sorry! No Web Storage support..');
}
location.reload();
});
});
</script>
版权声明:本文标题:javascript - How to reload page and update selection when a new value is selected in JQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742193719a2430689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论