日期:2014-05-17  浏览次数:20652 次

web开发,JavaScript 怎么获取struts1给定的元素 (非一般获取)
例:
public   class   User{
        String   name;
        String   age;
        //get   set

}
//action   省略     名字   叫   userAction


public   class   UserForm   extends   ActionForm{
        User   user;
        //get   set
}

//jsp
function   fun(){
        var   name=document.getElementById( " ");//这里如何获取user.name
        var   age=document.getElementById( " ");//这里如何获取user.age
}

  <html:form   action= "userAction.do "   target= "rightMiddleMess "     onsubmit= "return   fun(); ">
        姓名: <html:text   property= "user.name "   styleId= "name "> </html:text>
        年龄: <html:text   property= "user.age "   styleId= "phone "> </html:text>
    <html:submit> 添加 </html:submit>
       
        </html:form>



------解决方案--------------------
userAction中
Java code
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        return mapping.findForward("success");
    }

------解决方案--------------------
如:
<html:form action="userAction.do">
<html:text property="age" size="50">
</html:form>

这个时候可以发现,因为 html:text中没有id这个属性,有name这个属性,但是这个name属性和<input >的name属性含义完全不一样,因此不能通过document.getElementsByName("age");来进行输入值的获取。

此时可以采用下面的方式来进行该标签值的获取:
var age= document.all['age'].value;

//document.all['标签的property对应名称'].value
这种方式就能获取到struts的html标签下的form表单中的标签值,然后通过js方法来进一步做判断。

还有一种方法就是
var testform=document.forms[0];
然后通过testform.来进行对应标签值的获取。比如
testform.age.value。
不过我在使用上面方法的时候没有通过,可能有些操作失误。

希望大家有好的方法的话都能说出来,共同研究,一起提高。