登陆表单页面的制作和校验

制作的效果图:

制作思路:

整个表单框在div控件中,可把此表单看作是一个十行三列的表格。

要用到的控件:<form></form>表单控件  <table></table>表格控件 <tr></tr>行控件 <td></td>单元格控件

<input/>控件

校验思路:

当焦点离开第一行时,判断第一行是否为空,若为空,提醒用户。以下雷同。

主要用到的事件:onblur(失去焦点事件)

源码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        
        <link rel="stylesheet" href="css/表单.css" />
        <script type="text/javascript">
            function shiqu(id,id2)
            {  
//            alert("第一行失去焦点");
            var idElement = document.getElementById(id);
             var v = idElement.value;
//             alert(typeof v);
        if(v == "")
        {   
        
        
        var id2Element = document.getElementById(id2);
        id2Element.style.display = "inline";
        
        }
        else{  
        var id2Element = document.getElementById(id2);
        id2Element.style.display = "none";
        
        }
            }
        </script>
        
        
    </head>
    <body>
    
    <div id = "div001" style="
    border: 5px solid gray;
    width: 60%;
    height: 600px;">
<form>
    <table>
        <tr>
            <th id="001"><h2>会员注册</h2></th>
            <td colspan="2">USER REGISTER</td>
            
        </tr>
        <tr>
            <td>用户名:</td>
            <td>
                <input type="text" name="username001" id="username001" onblur="shiqu('username001','username001001')" />
            </td>
            <td style="display: none;"id = "username001001"><font color="#FF0000">用户名为空</font></td>
            
        </tr>
        <tr>
            <td>密码:</td>
            <td ><input type="password" name="password" id="password001" onblur="shiqu('password001','password001001')" /></td>
            <td style="display: none;" id="password001001"><font color="#FF0000">密码不能为空</font></td>
        </tr>
        <tr>
            <td>确认密码:</td>
        <td colspan="2"><input type="password" name="qrpassword" id="qrpassword001" /></td>
        
        </tr>
        <tr>
            <td>Email:</td>
            <td><input type="text" name="email" id="email001"onblur="shiqu('email001','email001001')" /></td>
        <td style="display: none;" id="email001001"><font color="#FF0000">email不能为空</font></td>
        </tr>
        <tr>
            <td>性别:</td>
            <td  colspan="2">
            男<input type = "radio"  name = "xingbie" id = "xingbie001"/>
            女<input type ="radio" name = "xingbie" id = "xingbie002" checked="checked"/></td>
        </tr>
        <tr>
            <td>出生日期:</td>
            <td colspan="2"><input type="text" name="birth001" id="birth001"onblur="shiqu('birth001','birth001001')" /></td>
            <td style="display: none;"id="birth001001"><font color="#FF0000">出生日期不能为空</font></td>
        </tr>
        <tr>
            <td>验证码:</td>
            <td><input type="text" name="yanzhengma001" id="yanzhengma001" /></td>
            <td><input type="image" name="tupian001" id="tupian001"src="../img/captcha.jhtml"onblur="shiqu('tupian001','tupian001001')"/></td>
            <td style="display: none;"id="tupian001001"><font color="#FF0000">验证码不能为空</font></td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" name="tijiao001" id="tijiao001" value="提交" /></td>
            
        </tr>
        
    </table>
    
</form>
    </div>
    </body>
</html>

 

### 表单提交后实现页面跳转的方法 在 HTML 和 JavaScript 中,可以通过多种方式实现在表单提交后页面自动跳转的功能。以下是几种常见的方法及其具体实现: #### 方法一:通过 `window.location.href` 实现跳转 当表单数据验证完成后,可以使用 JavaScript 的 `window.location.href` 属性来指定目标 URL 完成页面跳转[^3]。 ```javascript osub.onclick = function () { if (t1 && p1 && p2 && t2 && t3) { // 验证所有字段是否有效 window.location.href = "https://example.com/target-page"; // 跳转到目标页面 } else { alert("请填写正确的信息!"); } }; ``` 此方法适用于需要动态控制跳转逻辑的情况,例如基于用户输入的有效性决定是否执行跳转。 --- #### 方法二:利用 `<form>` 标签的 `action` 属性 HTML 表单本身支持通过设置 `action` 属性,在提交时直接跳转至指定的目标地址[^2]。 ```html <form action="https://example.com/target-page" method="POST"> <!-- 输入字段 --> <input type="text" name="username" placeholder="用户名"> <button type="submit">提交</button> </form> ``` 在此情况下,浏览器会在表单提交后默认加载由 `action` 指定的 URL 页面。 --- #### 方法三:结合 PHP 后端处理返回重定向响应 如果希望在服务器端处理完请求后再进行页面跳转,则可以在 PHP 文件中发送 HTTP 重定向头信息[^1]。 ```php <?php // 假设这是接收表单数据的PHP脚本 if ($_SERVER["REQUEST_METHOD"] === "POST") { $isValid = validateFormData($_POST); // 自定义函数用于校验数据有效性 if ($isValid) { header("Location: https://example.com/success-page"); exit(); } else { echo "提交失败,请检查您的输入!"; } } ?> ``` 注意:调用 `header()` 函数前不能有任何输出(包括空白字符),否则会引发报错。 --- #### 方法四:借助 AJAX 提交表单手动触发跳转 对于更复杂的场景,比如不想刷新整个页面而仅更新部分内容的同时还能实现跳转,可采用 AJAX 技术异步提交表单数据,根据服务端反馈结果调整行为[^2]。 ```javascript $(document).ready(function() { $('#myForm').on('submit', function(e) { e.preventDefault(); // 阻止默认提交动作 $.ajax({ url: $(this).attr('action'), type: 'POST', data: new FormData(this), processData: false, contentType: false, success: function(response) { if (response.success) { window.location.href = response.redirectUrl; // 根据后台返回的数据跳转 } else { alert('提交失败!'); } }, error: function(xhr, status, error) { console.error(error); } }); }); }); ``` 以上代码片段展示了如何利用 jQuery 库简化 AJAX 请求过程以及后续操作。 --- ### 总结 不同的需求对应着不同层次的技术解决方案。如果是简单的静态网页开发可以直接运用 HTML 或基础 JS 完成任务;而对于交互性强的应用程序来说,则可能需要用到高级技术如AJAX或者配合后端编程语言共同协作达成目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

psjasf1314

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值