doujia1939 2013-06-16 17:38
浏览 37
已采纳

使用JavaScript验证的表单提交[关闭]

This is not working, I am new to PHP. When I run this PHP ,the values get passed to the second php without showing any blank space validation, I don't know what i did wrong. I tried to write the code from looking at various tutorials online but don't know what went wrong.

  <html>
  <body>
  <script type="text/javascript">
  function validateForm(){
var w=document.forms["myForm"]["t1"].value;
  var x=document.forms["myForm"]["t2"].value;
var y=document.forms["myForm"]["t3"].value;
var z=document.forms["myForm"]["s1"].value;

    if (w==null || w=="")
   {
  alert("Fill out");
  return false;
  }
  if (x==null || x=="")
  {
  alert("Fill out");
  return false;
  }
  if (y==null || y=="")
  {
  alert("Fill out");
  return false;
  }
  if (z==null || z=="")
  {
  alert("Fill out");
  return false;
  }
   }
   </script>
   </head>
  <body><form id="f1" method="post" action="2.php" onsubmit="return validateForm()">
   <h1>FORM</h1>
   <br>
   ID :<input type="text" id="t1" name="t1" />
    NAME :<input type="text" id="t2" name="t2" /><br>
    GENDER :
   MALE<input type="radio" id="t3" name="t3" value="MALE" />
   FEMALE<input type="radio" id="t3" name="t3" value="FEMALE"/>
   <br>COURSE
   <select name="s1" id="s1">
   <option value="B.tech">B.Tech</option>
   <option value="B.com">B.com</option>
   <option value="Bba">Bba</option>
    </select>
   <br>
   <input type="submit" id="b1" name= "b1" value="Login" />
   <input type="submit" id="b1" name= "b1" value="Cancel" />
    </form>
      </body>
    </html>
  • 写回答

3条回答 默认 最新

  • duanjiu1003 2013-06-16 17:42
    关注

    There actually 2 problems with your code.

    1.
    You are trying to retrieve the form element using document.forms["myForm"], but there seems to be no form named 'myForm'. You should add:

    ...
    <form ... name="myForm">
    ...
    

    2.
    You cannot get the value of a group of radio-buttons using the .value attribute. What you have to do is iterate through all elements of the group and see if the checked property is set.
    The easiest way is to create a utility function for this (see below) and then call it:
    var y = getRadioValue("myForm", "t3");

    function getRadioValue(formName, groupName) {
        var value = "";
        var radioGroup = document.forms[formName][groupName];
        for (var i = 0; i < radioGroup.length; i++) {
            if (radioGroup[i].checked) {
                value = radioGroup[i].value;
                break;
            }
        }
        return value;
    }
    

    If you are using jQuery, you can omit the function and just use the appropriate selector:

    var y = $('input[name="t3"]:checked').val();
    

    See, also, this short demo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀