0

我正在使用 php、html、javascript 进行项目。我正在使用 on blur 和 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>
4

1 回答 1

1

您缺少结束大括号(函数关闭)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 

检查控制台,你会得到错误

于 2013-02-03T06:00:56.433 回答