为何不能获得option的值
我在做一个翻页程序,其中一个功能是通过一个下拉框可以改变每页显示的记录数,并在改变时刷新页面,主要代码如下:
<script language= "javascript ">
function changepage(){
var page = document.formPagination.mselect.value;
window.location.href= "PaginationResult.jsp?pagenum= "+page;
}
</script>
<form name= "formPagination " action= "PaginationResult.jsp " method= "post ">
<select id= "mselect " name= "pagenum " onchange= "changepage() ">
<option> 5 </option>
<option> 10 </option>
<option> 20 </option>
</select>
</form>
但就是不能成功,每次刷新时地址栏都显示http://127.0.0.1:8080/c/PaginationResult.jsp?pagenum=
就是说pagenum没有获得值,我感觉问题就在var page =document.formPagination.mselect.value;这句话上,把它改为一个固定值就行,比如var page =10
------解决方案-------------------- <option> 5 </option>
<option> 10 </option>
<option> 20 </option>
你自己都根本没有赋 value 属性值,怎么可能会取到什么值?
------解决方案--------------------把你的option加个value
javascript方法:
selectIndex = document.getElementById( "mselect ").selectedIndex;
displayNumber=
document.getElementById( "mselect ").options[selectIndex].innerText.substring(0,2);
location.href = "PaginationResult.jsp?pagenum= "+displayNumber;
------解决方案--------------------那你就把post的字符串再接收一下就可以了呀
JSP的不清楚 原理应该都一样的。
IF(是POST的字符串的VALUE)
<option selected= "selected "> ... </option>
ELSE
<option> ... </option>
------解决方案-------------------- <option value=5> 5 </option>
改成这样就可以了
------解决方案--------------------对于
"如何才能让页面刷新后显示我选的那个选项 "的解决
function setSelected()
{
//获取查询字符串
var strQry = window.location.search;
var pageRex = /.*pagenum=([^&]+).*/;
pageRex.test(strQry);
//获取当前选择的pageNumber
var currentNumber = RegExp[ "$1 "];
var sel = document.getElementById( "mselect ");
for (var i = 0; i < sel.options.length; i++)
{
if (sel.options[i].value == currentNumber)
{
sel.selectedIndex = i;
break;
}
}
}
<body onload= "setSelected() ">