2

I am working on a project , the situation is , the client has to enter the details , and after he enters the details , with java script ( confirm dialogue ) , i m showing all the important details for verification , then when he press ok, the form get submitted.

this is what i have made , but seems it is not working... need help ...

( after pressing cancel .. the dialog box keeps on coming .. and worst part is finally the form get submitted even if i have selected cancel )

( i have removed other details ...)

   <script type="text/javascript">
function validateMyForm()
{
    var X=confirm("ARE DETAILS CORRECT ??");
  if(!X)
  { 
    alert("validation failed false");
    returnToPreviousPage();
    return false;
  }
else
 {alert("validations passed");
  return true;}
}
</script>





<form name="frm" action=<?php print "new_update_attendence.php"; ?> method="POST"    
id="content" onsubmit="return validateMyForm();">
4

3 回答 3

4

大写很重要!

var x=confirm("ARE DETAILS CORRECT ??");
if(!X) // change to if(!x)

对于问题的第二部分,您可以这样做:

<script type="text/javascript">
    function validateMyForm()
    {
        var X=confirm("ARE DETAILS CORRECT ??");
        if(!X)
        { 
            setTimeout((function(){ window.location.reload(); }), 500);
            return false;
        }
        else
        {
            return true;
        }
    }
</script>
于 2012-08-14T13:50:28.503 回答
3

X必须是小写:

 if(!x)
  { ...
于 2012-08-14T13:50:16.423 回答
1

我猜你过去使用过 jQuery。在 jQueryreturn false中会阻止表单一起提交。这是因为 jQuery 捕获了返回值 ( false) 并将其转换为eventObject.preventDefault(); eventObject.stopPropagation();while return false,没有 jQuery 只会执行第一个(阻止该事件的默认操作)。该事件仍然冒泡/传播。

将您的 HTML 更改为onsubmit="return validateMyForm(event);">以访问事件对象。然后,在 JavaScript 中:

function validateMyForm(e)//e should hold the event
{
    e = e || window.event;//except for older IE versions(?), which we correct here
    //do whatever, if the form shouldn't be submitted:
    if (!confirm("ARE DETAILS CORRECT ??"))
    {
        if (e.preventDefault)
        {//chrome, FF, opera, ...
            e.preventDefault();
            e.stopPropagation();//<-- important line here
            return false;
        }//cruddy IE clients
        e.returnValue = false;
        e.cancelBubble = true;//<-- stop event in its tracks
        return false;
    }
}

那应该这样做。

于 2012-08-14T14:14:03.593 回答