0

我有一个表单:<form id="form" action="updatescore.php" method="post">和一个 php 文件:updatescore.php其中包含使用表单中的输入值更新数据库的代码。这一切都在使用提交按钮时起作用。

现在,如果 javascript 语句为真,我想删除提交按钮并提交表单。

js代码部分为:

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
                        }
                    });
                }

但是,如果该语句为真并且数据库未更新,则不会发生任何事情。关于这个有什么想法吗?

亲切的问候,

4

3 回答 3

0

你确定条件是真的吗?

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

这也是不对的:

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

也许:

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

或者:

if ($('#gone')[0].style.display=="none") {
于 2012-05-26T12:54:58.823 回答
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
                    }
                });

document.getElementById('uhs').innerHTML > 0 不是事件,因此无法运行您的代码。

试试这个.....

   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
                    }
                });
            }
    });
于 2012-05-26T13:07:40.410 回答
0

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!

于 2012-05-26T20:21:18.357 回答