简体   繁体   中英

Call function on submit button click

I have this html text input and javascript code:

<input type="text" name="statuspopup" id="statuspopup" value="" />
<script type="text/javascript">
function SubmitTicketForm() {
    return CheckRequired();

    if($('#statuspopup').val() == '') {
        //do status popup here
        alert("status");
    } else {
        //just submit the form
        $('form1').submit();
    }
}
</script>

I then have this submit button:

<input type="submit" onclick="SubmitTicketForm();" name="submit" id="submit" value="Save" />

so it should show a javascript alert, but its not doing it.

After you immediately return CheckRequired() function call your function stop executed. Supposed it returns BOOL value your function should look like this:

function SubmitTicketForm() {
    if( CheckRequired() === false ) {
        return false;
    }

    if($('#statuspopup').val() == '') {
        //do status popup here
        alert("status");
    } else {
        //just submit the form
        $('form1').submit();
    }
}

Also seems like your form selector is wrong. There is no such html element as form1 if it is ID it should be #form1 or .form1 if it's class name

Function ends after return statement. Everything written after return in function is ignored. Place return at last.

<input type="text" name="statuspopup" id="statuspopup" value="" />
<script type="text/javascript">
function SubmitTicketForm() {
    if($('#statuspopup').val() == '') {
        //do status popup here
        alert("status");
    } else {
        //just submit the form
        $('form1').submit();
    }
return CheckRequired();
}
</script>

Put the return at the end of the function like this:

function SubmitTicketForm() {

    if($('#statuspopup').val() == '') {
        //do status popup here
        alert("status");
    } else {
        //just submit the form
        $('form1').submit();
    }

    return CheckRequired();
}
<input type="submit" onclick="SubmitTicketForm();" name="submit" id="submit" value="Save" />

<script type="text/javascript">
function SubmitTicketForm() {
    if($('#statuspopup').val() == '') {
        //do status popup here
        alert("status");
    } else {
        //just submit the form
        $('form1').submit();
    }

     return CheckRequired();
}
</script>

now replace it thank you

ok, another variant... this ASSUMES that checkrequired is checking the status of the checkbox(and returns a bool) and you only wish to have the alert if checkbox is not checked AND there is a value in statuspopup

function SubmitTicketForm() 
{
    if(!CheckRequired() && $('#statuspopup').val() == '') {
        //do status popup here
        alert("status");
        return false;
    } else {
        //just submit the form
        //$('form1').submit();
        return true; //it's a submit button...will submit by itself. if you want to 
             //manually submit with jquery, make it a button
    }
}

and don't forget to add a return to your onclick:

<input type="submit" onclick="return SubmitTicketForm();" name="submit" id="submit" value="Save" />

once again, knowing what checkreturn is and why it's there would be useful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM