使用jQuery自动提交不起作用的表单

使用jQuery自动提交不起作用的表单

问题描述:

I have a form: <form id="form" action="updatescore.php" method="post"> and a php file:updatescore.php which contains the code to update the database using the input values from the form. This all works when using a submit button.

Now I want to remove the submit button and submit the form if a javascript statement is true.

The js code part is:

if (document.getElementById('uhs').innerHTML > 0) { //this is true because the div gone is hidden
                    $('#gone').hide();
                    $.ajax({
                        type: "POST",
                        data: $("#form").serialize(),
                        cache: false,
                        url: "updatescore.php",
                        success: function () { //if submit to db is done
                            getUsers(1); //a function to reload a page overview
                        }
                    });
                }

But nothing happens if the statement is true and the database is not updated. Any ideas on this one?

Kind regards,

Thanks for the replies. The code worked, but it seemed that a bug in another part of the code was messing with the AJAX call. So in the end there was nothing wrong with the code at all.

Again, thanks for the replies and suggestions!

are you sure the condition is TRUE?

if (document.getElementById('uhs').innerHTML > 0) { //this is true because the div gone is hidden
   alert('its true');

also that is not right:

if (document.getElementById('uhs').innerHTML > 0)   

perhaps:

if (document.getElementById('uhs').innerHTML.length > 0)

or:

if ($('#gone')[0].style.display=="none") {

The submit button is what triggers the event that runs the code:

 $('#gone').hide();
                $.ajax({
                    type: "POST",
                    data: $("#form").serialize(),
                    cache: false,
                    url: "updatescore.php",
                    success: function () { //if submit to db is done
                        getUsers(1); //a function to reload a page overview
                    }
                });

The document.getElementById('uhs').innerHTML > 0 is not a event so there is not way to run your code .

Try this.....

   document.addEventListener('keyup', function (e) {
           //
          if (document.getElementById('uhs').innerHTML > 0) { 
                $('#gone').hide();
                $.ajax({
                    type: "POST",
                    data: $("#form").serialize(),
                    cache: false,
                    url: "updatescore.php",
                    success: function () { //if submit to db is done
                        getUsers(1); //a function to reload a page overview
                    }
                });
            }
    });