[英]jQuery change selected value and submit form basd of localStorage value
I have a problem. 我有个问题。 I have select with name category and have code:
我选择了名称类别并具有代码:
if (localStorage.getItem('category') !== null) {
$('select[name=category]').val(localStorage.getItem('category')).trigger('change');
$('.form-search').submit();
}
If in Local storage I have id, then I change selected value in select. 如果在本地存储中有ID,则在select中更改所选值。 But How I can submit this form?
但是如何提交此表格? My code is not working, because page reloading many times..
我的代码无法正常工作,因为页面重新加载了很多次。
MY form: 我的表格:
<form method="get" class="form-search">
....
<select name="category">
<option value="1">Auto</option>
<option value="2">Sport</option>
...
</select>
</form>
Because the form hasn't action
, browser submit form to this page and page reloaded and because you don't removed localStorage
the code runned again. 由于表单没有
action
,浏览器将表单提交到此页面并重新加载页面,并且因为您没有删除localStorage
因此代码再次运行。 So you should remove localStorage category
before submitting form. 因此,您应该在提交表单之前删除localStorage
category
。
var category = localStorage.getItem('category');
if (category !== null) {
$('select[name=category]').val(category).trigger('change');
localStorage.removeItem('category');
$('.form-search').submit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.