onblur和onsubmit功能不起作用有人可以帮我在这里

时间:2013-02-03 05:52:14

标签: javascript html

我正在使用php,html,javascript进行项目。我正在使用模糊和提交两个函数来提供动态的错误感觉。以下逻辑在除此之外的每个文件中都有效。 像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>

1 个答案:

答案 0 :(得分: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 

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