-2

我已经处理了半天了。我的功能不起作用。它曾一度奏效,现在我不知道如何回去。这是我的 HTML 代码

<a href="#" onclick="updateDB('<?php echo $a; ?>','<?php echo $b; ?>','<?php echo $c; ?>','<?php echo   
$d; ?>','<?php echo $e; ?>','<?php echo $f; ?>','<?php echo $g; ?>','<?php echo $h; ?
>','<?php echo $i; ?>','<?php echo $j; ?>','<?php echo $k; ?>')">Test</a>

这是我的javascript代码

<script>    
$(function() {

   function updateDB(a,b,c,d,e,f,g,h,i,j,k)
   {
        $.post("update.php",
        { a:a,b:b,c:c,d:d,e:e,f:f,g:g,h:h,i:i,j:j,k:k 
        });

    alert("Finally");
   }
 }
</script>

我不能说最后。

任何帮助是极大的赞赏。

谢谢,

4

1 回答 1

1

Because you are using an <a> tag, you need to stop the default execution of such a link. The easiest would be:

onclick="updateDB(...); return false;"

That being said, since you already use jQuery, do yourself a favor and use jQuery's event handlers, e.g:

<a href="#" id="testlink">Test</a>

<script>
    $('#testlink').click(function(event){
        event.preventDefault();
        updateDB('<?php echo $a; ?>','<?php echo $b; ?>','<?php echo $c; ?>','<?php echo   $d; ?>','<?php echo $e; ?>','<?php echo $f; ?>','<?php echo $g; ?>','<?php echo $h; ?>','<?php echo $i; ?>','<?php echo $j; ?>','<?php echo $k; ?>');
    });
</script>

Even better would be to attach the variables you get from PHP and add them to a data- attribute, so you can reuse the click handler:

<a href="#" class="testlink" data-my-value="1">Test 1</a>
<a href="#" class="testlink" data-my-value="2">Test 2</a>

<script>
    $('.testlink').click(function(event){
        event.preventDefault();
        var data = $(this).attr('data-my-value');
        updateDB(data);
    });
</script>

UPDATE: To still redirect to another page, I would do this (untested):

<a href="newpage.html" class="testlink" data-my-value="1">Test 1</a>

<script>
    $('.testlink').click(function(event){
        event.preventDefault();
        var data = $(this).attr('data-my-value'),
            url = $(this).attr('href');
        updateDB(data, url);
    });

    function updateDB(data, url)
    {
        $.post("update.php",
            data, 
            function() {
                window.location = url;
            }
        );
    }
</script>
于 2013-07-21T19:06:14.377 回答