1

simple if/else took a dump on me.....it was working fine(copied from prev proj), made some changes and now nothing.

i deleted it all and wrote a simple function to check for empty and nothing, the form is still going to next page...and it was JUST working.

so now i make the simplest possible one and so...its still messed up so, what am i missing. should b simple.

i need a break lol

heres the code.

html:

<form name="mainForm" action="formProc.php" method="get"  id="mainForm" onSubmit="return allChecks();" >


<input type="text" name="name" id="name"/>

</form>

js:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready( function(){
    //formVars
    var name = $('#name');

function allChecks(){
    if(name.val()==""){
        alert("lopan"); 
        return false;
    }

        if(name.val()===""){
        alert("lopan"); 
        return false;
    }

        if(name.val()== null){
        alert("lopan"); 
        return false;
    }
return false;
}

});
</script>

As you can see...im basically TRYING to force a "false" on the form and it STILL goes onto the next page.

so dummying it down even further.

i create another button in the form with type="button", declare it and well, this

var btn= $('#clearF');

btn.click(allChecks);

Then this works...so im lost.

edit*** im trying to force a false/prevent from going to next page because, as i was adding validation to the fields, i noticed it went through to next page whether value was true or not...after troubleshooting a while i decided to see why its going to next page by itself...hence the trying to force false and failing the prevent it from going to next page.

***end edit

Any tips/ideas/point out some DUMB thing im doing....dont hesitate to point it out Thanks in advanced.

4

4 に答える 4

3

allChecksマークアップに対して「表示」されません。$(document).ready()ハンドラーの内部にあります。「表示」するには、次のように宣言します。

window.allChecks=function(){ 
于 2012-04-15T11:06:21.197 に答える
0

マークアップの代わりにonsubmit、ハンドラーの作成にjQueryを使用してみませんか?

$(document).ready(function(){

    $('#mainForm').on('submit',function(){
        //whatever you want to do on submit
    });

});
于 2012-04-15T11:08:37.793 に答える
0

次の方法で送信イベントを処理できます。

$(document).ready( function(){
    //formVars

    var name = $('#name');

    $("#mainForm").submit( function() {

    if ( name.val() === "" ) {
        alert("lopan");
        return false;
    }

    else {
      return true;
    }
  });
});

http://jsfiddle.net/YMg9V/1/

于 2012-04-15T11:15:51.293 に答える
0

少し前に、送信すると突然動作が変わる生産的なコードがありました。私が今やっていることは、あなたの(かなり普通の)コードの代わりです。

<form name="mainForm" action="formProc.php" method="get"  id="mainForm" >
<input type="text" name="name" id="name"/>
var btn= $('#clearF');
btn.click(allChecks);

$(document).ready( function(){
    //formVars
    var name = $('#name');

function allChecks(){
    if(name.val()==""){
      alert("lopan"); 
      return false;
    }else{
      mainForm.submit();
    }
return false;
}

});
</script>
于 2012-04-15T11:16:17.767 に答える