Determine the value of the pressed submit button with multiple submit buttons

I have a situation where I need to track which button of the submit button I need to do in order to set the variables accordingly. Below is the test code,

<script>
function submitForm(form) {
    alert(document.getElementById('sb').value);
    if (document.getElementById('sb').value=="One") {
        //Do something
    }
    return true;
}
</script>


<form action="" method="get" onsubmit="return submitForm(this);">
    <input type="submit" name="sb" value="One">
    <input type="submit" name="sb" value="Two">
    <input type="submit" name="sb" value="Three">
</form>

A warning always shows “One,” even if I press the “Two” or “Three” button. But the URL is changed using the clickable parameter. How to warn the value that is in the pressed submit button?

Note. I need a solution without jQuery

EDIT: I change the bit of code that onsubmit calls submitForm (this); The problem is even to use document.forms [0] .sb.value its undefined because document.forms [0] .sb returns a node list of all submit buttons just like document.getElementById ('sb')

+5
5

:

<script>
function m(value) {
alert(value);
}
</script>

<input type="button" value="One" onClick="m(this.value)">
<input type="button" value="Two" onClick="m(this.value)">
<input type="button" value="Three" onClick="m(this.value)">

, , , id:

<input type="button" id='myId' value="Three" onClick="m(this.id)">
0

. onclick javascript.

+1

You can try this,

    <form>
        <input class="myButton" type="submit" name="sb" value="One">
        <input class="myButton" type="submit" name="sb" value="Two">
        <input class="myButton" type="submit" name="sb" value="Three">
    </form>

    <script type="text/javascript">
        $(".myButton").on('click', function() {
        alert($(this).val());
        });
    </script>
+1
source

I am a little new to javascript; please forgive me if I am wrong. Wouldn't that make a difference if your if statement had a 3 sign =?

Should it be:

if (document.getElementById('sb').value === "One") {
  //Do something
}
return true;
+1
source

you can try with jquery something like:

$(":submit").live('click', function() {
    alert($(this).val());
})
0
source

All Articles