当前页面: 开发资料首页 → JSP 专题 → 100分紧急求助!select multiple赋值问题
100分紧急求助!select multiple赋值问题
摘要: 100分紧急求助!select multiple赋值问题
我想在jsp页面上把值赋给select框,代码是这样的
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="com.boco.eoms.interfaces.sms.*"%>
<head>
test
</head>
<body onload="loadtext()">
<script language="javascript">
function loadtext(){
var soureOld = document.forms[0].test.value;
var soureNow = document.forms[0].mainSource.value;
if(soureOld.length==0)
{
//方法1
document.forms[0].test.value=soureNow;
//方法2
document.getElementById("test").innerSelectarea=soureNow;
}
}
</script>
<form method="POST">
<table>
<tr>
<td><input type="text" name="mainSource" value="222"/></td>
</tr>
<tr>
<td>
<select name="test" multiple="multiple" >
</select>
</td>
</tr>
</table>
</form>
</body>
两种方法都无法使select框得到值。请问是什么原因,或者哪种方法可以得到。十分感谢!
这个不会报错吗?“ document.forms[0].test”为空或不为对象
document.forms[0].test.value=soureNow的意思是把soureNow赋值给当前页面forms对象的一些属性和值.
顶。
但是涉及到页面初始化的先后问题,以前我做的时候出现了错误提示:为空或不为对象。
<head>
test
</head>
<body onload="loadtext()">
<script language="javascript">
function loadtext(){
var soureNow = document.forms[0].mainSource.value;
var obj=document.getElementById("test");
if(soureNow.replace(//s/g,"")!=""){
alert(soureNow);
obj.length=obj.length+1;
obj.options[obj.length-1].text=soureNow;
}
}
</script>
<form method="POST">
<table>
<tr>
<td><input type="text" name="mainSource" value="222"/>
<input type="button" onClick="loadtext();" value="添加"></td>
</tr>
<tr>
<td>
<select name="test" multiple="multiple" >
</select>
</td>
</tr>
</table>
</form>
</body>
问题搞定揭贴......
lip009(深蓝忧郁),强人,哈哈,给分!