Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提交前检查表单-JavaScript_Javascript_Html - Fatal编程技术网

提交前检查表单-JavaScript

提交前检查表单-JavaScript,javascript,html,Javascript,Html,我有以下表格: <html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Choose</title> <script type="javascript/text"> function ischecked()

我有以下表格:

<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Choose</title>
  <script type="javascript/text">

    function ischecked(){
        var check = document.getElementsByTagName( 'input' );
        for( var i = 0; i < check.length; i++ ){
        if( check[ i ].type == 'radio' && check[ i ].checked ){
            return true;
          }
        }
        return false;
    }

  </script>
</head>

<body>
  <form method="post" enctype="application/x-www-form-urlencoded" onsubmit="ischecked();">
    <h1>Choose</h1>

    <p><input type="radio" name="choose" value="psychology"><font size="5" color="#0033CC">Instant Psychology</font><br>
    <br>
    <input type="radio" name="choose" value="geography"><font size="5" color="#CC0000">Instant Geography</font><br>
    <br>
    <input type="radio" name="choose" value="gastronomy"><font size="5" color="#660033">Instant Gastronomy</font><br>
    <br>
    <input type="submit" name="Submit" value="Go"></p>
  </form>


</body><link rel="stylesheet" type="text/css" href="data:text/css,"></html>

选择
函数ischecked(){
var check=document.getElementsByTagName('input');
对于(变量i=0;i

即时地理

即食美食


在提交表单之前,我想确保其中一个单选按钮已被选中。但是,它不起作用,不管怎样,表单是提交的。这里我做错了什么?

您需要
从内联事件处理程序返回函数的结果。

您需要
从内联事件处理程序返回函数的结果。

您必须直接在属性值中对照验证函数返回的值进行检查。也就是说,在HTML表单声明中

<form method="post" enctype="application/x-www-form-urlencoded" onsubmit="ischecked();">
而不是:

onsubmit="ischecked();"

您必须直接在属性值中检查验证函数中返回的值。也就是说,在HTML表单声明中

<form method="post" enctype="application/x-www-form-urlencoded" onsubmit="ischecked();">
而不是:

onsubmit="ischecked();"

你能给我更多的细节吗?你能给我更多的细节吗?onsubmit=“return ischecked();”onsubmit=“return ischecked();”