有人說用隱藏域保存value,給select觸發onchange事件后改變隱藏域的值,可是我用這個方法寫在頁面刷新之后隱藏域并不能保存上面的值(是我寫錯了 還是這個方法是不可行的?)html的代碼<select class="form-control" id="search-select" onchange="change()">
<option value="course" selected>課程</option>
<option value="major">專業</option>
<input type="hidden" name="selectVal" value="course" id="selectVal">
</select>js的代碼var selectVal=document.getElementById("selectVal"); var select=document.getElementById("search-select");
selectVal.value=select.value;
alert(selectVal.value); function change(){ var val=select.options[select.selectedIndex].value;
selectVal.value=val; //用隱藏域存數據
} for(var i=0;i<select.options.length;i+=1){ if(select.options[i].value==selectVal.value){ select.options[i].selected=true; break;
}
}接觸js不多+ +有點懵逼 除了從后臺返回數據和用Ajax 請問各位大神還有其他方法嗎?
頁面刷新后怎么讓select保持選中上一個選定值,可以用隱藏域保存嗎
largeQ
2018-08-11 09:09:04