<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head> <script type="text/javascript" language="javascript">
function check(){
if(document.getElementById("name").value==""){
alert("用户名为空");
return false;
}else{
return true;
   }
}
</script>
<body>
<h1>
</h1>
<h1>
添加用户
</h1>
<form name="form1" action="loginok.html" method="post"> 中文用户名:
<input id="name" type="text" name="adminUser.name" />
span> * 密码:
<input id="password" type="text" name="adminUser.password" />
span> * 

城市:
<select name="adminUser.city">
<option value="0">
请选择
</option>
<option value="北京">
北京
</option>
                                     <option value="上海">
上海
</option>
</select>

地址:
<input type="text" name="adminUser.address" />

<input type="submit" value="保存" onclick="check()" />
</form>
</body>
</html>我的项目是ssh,strut1.2的,所以name那里是用formbean绑定问题一:为什么我点击保存的时候,"用户名为空"弹出来,还是会跳到form表单指定的action呢?
问题二:弹出对话框后,点击对话框后,我刚选的城市下拉列表是北京,页面闪了一下又跳回到"请选择"了,怎么办才能让他保留刚才的选择?
问题三:就是帮忙写个验证,弹出信息也好,怎么样都好就是让用户一定要输入和选择城市才能通过。。

解决方案 »

  1.   

    <html>
        <head>
        </head>    <script type="text/javascript" language="javascript">
        function check(){
            if(document.getElementById("name").value==""){
                alert("用户名为空");
                return false;        
            }else{
                return true;
              }
        }
    </script>
        <body>
            <h1>
                        </h1>
            <h1>
                添加用户
            </h1>
            <form name="form1" action="loginok.html" method="post" onsubmit="return check()">            中文用户名:
                <input id="name" type="text" name="adminUser.name" />
                 密码:
                <input id="password" type="text" name="adminUser.password" /> 
                
                城市:
                <select name="adminUser.city">
                    <option value="0">
                        请选择
                    </option>
                    <option value="北京">
                        北京
                    </option>
                                         <option value="上海">
                        上海
                    </option>
                </select>
                
                地址:
                <input type="text" name="adminUser.address" />
                
                <input type="submit" value="保存"/>                
            </form>
        </body>
    </html>用submit的话,就要在提交表单之前返回。要想用onclick的话把表单提交换成button类型的,在js中提交表单。下面方法:<html>
        <head>
        </head>    <script type="text/javascript" language="javascript">
        function check(){
            if(document.getElementById("name").value==""){
                alert("用户名为空");
                return false;        
            }else{
                document.form1.submit();
              }
        }
    </script>
        <body>
            <h1>
                        </h1>
            <h1>
                添加用户
            </h1>
            <form name="form1" action="loginok.html" method="post">            中文用户名:
                <input id="name" type="text" name="adminUser.name" />
                 密码:
                <input id="password" type="text" name="adminUser.password" /> 
                
                城市:
                <select name="adminUser.city">
                    <option value="0">
                        请选择
                    </option>
                    <option value="北京">
                        北京
                    </option>
                                         <option value="上海">
                        上海
                    </option>
                </select>
                
                地址:
                <input type="text" name="adminUser.address" />
                
                <input type="button" value="保存" onclick="check()"/>                
            </form>
        </body>
    </html>