Skip to content Skip to sidebar Skip to footer

Print Value On Form Submit

I'm new to JavaScript. How can I print the values when an HTML form is submitted?

Solution 1:

Assuming your form looks like :

 <form action="">
        <input type="text" name="User" />
        <input type="password" name="Password"/>
        <span>Admin</span> <input type="checkbox" name="IsAdmin" />
        <span>US</span> <input type="radio" name="Country" value="US" />
        <span>UK</span> <input type="radio" name="Country" value="UK" />
        <input type="submit" />
    </form>

Jquery code should look like this as starting point :

<script  type="text/javascript">
        $(document).ready(function () {
            $("form").submit(function () {
                User = $("input[name='User']").val();
                Password = $("input[name='Password']").val();
                IsAdmin = $("input[name='IsAdmin']").attr("checked");
                Country = $("input:radio[name='Country']:checked").val(); /* Special case of radio buttons*/

                document.writeln(User);
                document.writeln(Password);
                document.writeln(IsAdmin);
                document.writeln(Country);

                return false;
            })
        });
    </script>

Solution 2:

jQuery code is below:

$('#button_ID').submit(function() {
 alert('something');
});

button_ID is id of your submit button if you want to stop form to submit then add return false; at end

$('#button_ID').submit(function() {
 alert('something');
 return false;
});

Post a Comment for "Print Value On Form Submit"