1

I have a complex website setup so I'll just make an example a simple one.

Current Setup

I have two buttons.

<a id="one" href="#">Link 1</a>
<a id="two" href="#">Link 2</a>

And I have two divs

<div id="showOne" style="display:none">1</div>
<div id="showTwo" style="display:none">2</div>

This is my JQuery code

$('#one').click(function (e) {
    $('#showOne').show();
});

$('#two').click(function (e) {
    $('#showTwo').show();
});

What I'm Trying to Accomplish

Basically, I have a database table setup that has a row to count how many times was div(showOne) and div(showTwo) shown.

How would I work with AJAX to add one to the database row aka counter if display = block?

My Attempt

$('#one').is(":visible")
{

$.ajax({

    type:"POST"
    url: update.php
    data: {type:link_one_shown}
    success: function(data){
      alert("Test");
    }

})

When I do this, the rest of my JQuery code crashes. Not sure if this doesn't make sense or I just wrote something wrong.

Thank you in advance!

4

2 回答 2

2

我可以看到的三个问题:

  1. 正如 Akam 所指出的,您已经破坏了if声明。
  2. data不正确 -应该是link_one_shown变量(您尚未定义?)或字符串文字。
  3. 您在ajax函数参数之间缺少逗号。

这是修改后的代码:

if ($('#showOne').is(":visible")) {
    $.ajax({
        type:"POST",
        url: update.php,
        data: { type: "link_one_shown" },
        success: function(data){
          alert("Test");
        }
    });
}
于 2013-07-20T00:44:10.313 回答
0

为什么不只是将ajax调用移动到函数并在显示div时调用它,这样可以避免代码重复并使其更简单。

$('#one').click(function (e) {
    $('#showOne').show();
    updateDB('link_one');
});

$('#two').click(function (e) {
    $('#showTwo').show();
    updateDB('link_two');
});


function updateDB(link)
{
    $.ajax({

        type:"POST"
        url: update.php
        data: {type:link}
        success: function(data){
          alert("Test");
        }

    });
}
于 2013-07-20T05:06:54.230 回答