在javascript中访问表单变量 [英] Accessing a form variable inside javascript

查看:106
本文介绍了在javascript中访问表单变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码

 < html> 
< head>标题
< script>
函数callme()
{
alert(Hi);
document.test.action =testAction.do;
alert(document.getElementById(option).value);
alert('<%= request.getParameter(option)%>');
}
< / script> }
< / head>
< body>
< FORM method =postname =test>
< select name =optionid =option>
< option> 1< / option>
< option> 2< / option>
< / select>
< input type =submitvalue =SubmitonClick =callme()>
< / form>
< / body>
< / html>

这是我必须做的一个样本jsp代码。问题是我能够得到值为document.getElementById,但我想要在我的操作类内部访问此参数,该操作类将被重新调用为null。任何人都可以帮助我解决此问题。 解决方案

听起来有点不清楚。我认为你所说的是这一行:

  alert('<%= request.getParameter(option)) %GT;'); 

在警报中显示为空吗?



我认为主要原因是该表格尚未提交。您正在使用服务器端请求对象,但是您呈现此页面时尚未提交任何内容,因此request.getParameter(option)返回null。



如果您提交这个表单,然后在testAction.do里面(因为这是它将被提交到的地方),你将能够从request.getParameter(option)中读取值。

I have a code like this

<html>
<head>Title
<script>
function callme()
{ 
        alert("Hi");
              document.test.action ="testAction.do";
        alert(document.getElementById("option").value);     
        alert('<%=request.getParameter("option")%>'); 
}
</script>           }
</head>
<body>
<FORM method="post" name="test" >
<select name="option" id="option">
<option>1</option>
<option>2</option>
</select>
<input type="submit" value="Submit" onClick="callme()">
</form>
</body>
</html>

This is a sample jsp code of what I have to do.The problem is I am able to get value for the document.getElementById but I want to access this parameter inside my action class which is retunring as null.Can anyone help me in solving this issue.

解决方案

Sounds a bit unclear. I presume what you are saying is that this line:

alert('<%=request.getParameter("option")%>');

shows null in the alert?

I think the main reason is that the form hasn't been submitted yet. You are using a server side "request" object, but you render this page nothing has been submitted yet hence request.getParameter("option") returns null.

If you submit this form then inside testAction.do (since this is where it will be submitted to) you will be able to read the value from request.getParameter("option")

这篇关于在javascript中访问表单变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆