onblur和onsubmit功能不起作用,有人能帮我吗

the onblur and onsubmit function are not working could someone please help me out here

本文关键字:onsubmit 功能 不起作用 onblur      更新时间:2023-09-26

我正在使用php、html、javascript进行项目。我使用onblur和onsubmit这两个函数来为错误提供动态感觉。以下逻辑适用于除此文件之外的所有文件。onsubmit和onblur等表单功能不起作用,我也使用了警报进行检查,但它没有被调用。有人能检查一下并告诉我哪里出了问题吗

<script language="Javascript">
//the flags are used to check if all the fields are filled and none is empty
var flag1;
var flag2;
var flag3;
function test(){
    alert("fn called");
    return true;

}
function fun1() 
{
var donated_date=document.forms["myform"]["donated_date"];
if(donated_date==null||donated_date=="")
{   
    document.getElementById('ddate').innerHTML = "Choose the date of your last donation " ;
    document.getElementById('ddate').style.display = "block" ;
    flag1=false;
    return false;
}
document.getElementById('ddate').style.display = "none" ;
flag1=true;
return true;
}
function fun2()
{   
var location=document.forms["myform"]["location"];
if(location==null||location==""||!isNaN(location))
{
    document.getElementById('loc').innerHTML = "Mention the location of your last donation " ;
    document.getElementById('loc').style.display = "block" ;

    flag2=false;
    return false;
}
document.getElementById('loc').style.display = "none" ;
flag2=true;
return true;
function fun3()
{   
var hospital_name=document.forms["myform"]["hospital_name"];
if(hospital_name==null||hospital_name==""||!isNaN(hospital_name))
{
    document.getElementById('hname').innerHTML = "Mention the Hospital Name where you last donated " ;
    document.getElementById('hname').style.display = "block" ;
    flag3=false;
    return false;
}
document.getElementById('hname').style.display = "none" ;
flag3=true;
return true;
}
function validate()
{
alert("hello");
}
</script>
          <form name="myform" method="post" action="o.php"  onsubmit="return validate()">
          <table width="94%" cellpadding="3px" class=" lastDonatedblood_table" cellspacing="0" style="border: 1px solid #CCCCCC;margin: -18px 0 14px 6px;width: 98.5%;">
            <tr>
                    <td style="text-align:left;">Donated Date:</td>
                    <td style="text-align:left;">
<input type="text" name="donated_date" id="donated_date"  onblur="return test();" style="width:300px;"/>
                        <div id="ddate"></div>
                    </td>
                </tr>
                <tr>
                    <td style="text-align:left;">Location:</td>
                    <td style="text-align:left;"><textarea  name="location" id="location"  onblur="return fun2();" style="width:300px;"></textarea></td>
                </tr>
                <center></center>
                <tr>
                    <td style="text-align:left;">Hospital Name:</td>
                    <td style="text-align:left;"><input type="text" name="hospital_name" id="hospital_name"  onblur="return fun3();" style="width:300px;"/></td>
                </tr>
                <center><div id="hname"></div></center>
                <tr>
                    <td style="text-align:left;">Type of Donation:</td>
                    <td style="text-align:left;">
                        <select title="" name="donationtype" id="donationtype"  style="width:317px;">
               <option value="blood">Blood</option> 
               <option value="platelets">Platelets</option>
               </select>
                    </td>
                </tr>
                <tr >
                  <td colspan="2">
                     <input name="submit" id="submit" value="ADD" type="submit" style="margin-top:5px; margin-left:185px; font-size:14px; width:100px;">
                  </td>
                </tr>
            </table>
            </form>

您缺少fun2()、的右花括号(函数关闭)

function fun2()
{   
var location=document.forms["myform"]["location"];
if(location==null||location==""||!isNaN(location))
{
    document.getElementById('loc').innerHTML = "Mention the location of your last donation " ;
    document.getElementById('loc').style.display = "block" ;

    flag2=false;
    return false;
}
document.getElementById('loc').style.display = "none" ;
flag2=true;
return true;
} <-- added closing curly bracket here 

检查控制台,您将收到错误