老师给检查一下,还有个问题用提交按钮怎么做,一提交得不到value值无法判断

来源:2-13 编程练习

激情的樱木花道

2021-06-22 15:01:45

<!DOCTYPE html>

<html lang="en">


<head>

    <meta charset="UTF-8">

    <title>Location 对象</title>

</head>


<body>

    <form>

        用户名:

        <input type='text'  id='text'><br>

        密  码:

        <input type='password'  id='password'><br>

        <input type='button' value='提交' id='btn'>

    </form>

    <script>

        var text=document.getElementById('text');

        var password=document.getElementById('password');

        var btn=document.getElementById('btn');

        btn.onclick=function(){

            if(text.value=='张三'&&password.value=='123456'){

                window.location='http://www.imooc.com';

            }else{

                alert('不对');

            }  

        }

    </script>

</body>


</html>


写回答

1回答

好帮手慕久久

2021-06-22

同学你好,代码是对的,问题解答如下:

是想用type="submit"的input按钮吗?比如下面的代码:

http://img.mukewang.com/climg/60d18d8c09ee65f807130367.jpg

如果是的话,使用当前代码是可以获取到value值的。只是该按钮有默认提交行为,当输入内容不合法,页面弹出alert框时,点击alert框后,页面会刷新,导致看上去效果不对。

需要禁止默认提交这个行为,如下:

http://img.mukewang.com/climg/60d18e0c093d8d8008740418.jpg

同学试一下。

祝学习愉快!

0

0 学习 · 15276 问题

查看课程