javascript能否直接获得html表单数据

来源:4-3 多重判断(if..else嵌套语句)

阿皎就是茕兔丶

2016-07-04 11:20

<body>

<form action="index.html" method="post">请输入您的姓名

    <input type="text" name="age" id="age">

    <input type="submit" value="提交">

</form>

<script type="text/JavaScript">

var myage=document.getElementById("age").value;

if(myage<=44)

  {document.write("青年");}

else if(myage<=59) 

  {document.write("中年人");}

else if(myage<=89)

  {document.write("老年人");}

else        

  {document.write("长寿老年人");}

</script>

</body>

没成功(╯‵□′)╯︵┻━┻是哪错了还是根本就不行

写回答 关注

4回答

  • 圣婴Z
    2016-07-04 12:40:46
    已采纳

    你得写个function a,判断语句放在方法中,之后在提交的input中,来响应鼠标事件,onclick="a()"。 

    Molly喜 回复阿皎就是茕兔...

    点击submit是向浏览器发送表单数据。如果想让js处理数据,需要一个事件,比如在button中onclick事件。 并且纠正一下下面,innerHTML不能用于获取input的输入内容,因为input内容隐藏在value中,需要用.value获取。

    2017-12-11 14:59:49

    共 3 条回复 >

  • llo蛋蛋oll
    2016-07-04 16:12:58

    type=submit会提交到你的form表单定义的action中

  • 阿皎就是茕兔丶
    2016-07-04 15:49:18

    <form action="index.html" method="post">请输入您的年龄:

        <input type="text" name="age" id="age">

    </form>

    <input type="button" value="提交测试" onclick="a()">

    <script language="javascript">

        function a(){

            var myage= document.getElementById("age").value;

            if(myage<=44)

              {document.write("青年");}

            else if(myage<=59) 

              {document.write("中年人");}

            else if(myage<=89)

              {document.write("老年人");}

            else        

              {document.write("长寿老年人");}

        }

    </script>


  • 汉娜之眼
    2016-07-04 11:39:17

    val是获得value属性中的值,而不是内容

    卡兰尼克 回复阿皎就是茕兔...

    用innerHTML获得内容

    2016-10-28 10:18:54

    共 2 条回复 >

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468602 学习 · 21893 问题

查看课程

相似问题