Click handling for jQuery AJAX (with PHP)

Not sure how to correctly formulate this question, but I will give him a chance. (If you have a suggestion on how to formulate this correctly so that others can find it, tell me and I will edit it).

I want to know the best methods for processing clicks on a page, which are then processed using AJAX. I temporarily used code that looks like this in HTML:

<a href="#clickable">Click me!</a>

And in jQuery, I handle clicks by binding clickto href.

Of course, I know that you should not report this to the user .

So how do I handle these clicks?

+3
source share
3 answers

HTML , , Javascript . , Javascript .

, :

<a href="some/working/path.php" class="ajax">Meaningful description!</a>

( - ), Javascript.

event.preventDefault(), , Javascript.

, jQuery

  // Target only anchors with class "ajax"
$("a.ajax").click(function(event) { 

    // Handle AJAX etc here.
    // ...

    event.preventDefault(); // <== Don't navigate away from page
});
+1

, xD

, -

$("a").click(function() {
  // do some stuff
});

?

: .

- :

<a href='link' onClick='return MyClass.myFunction()'>keyword </a>

, FALSE JS, ( Ajax). JS , PHP, -)

0

I usually have a href link to a static php page, and then use javascript to change the href link to the ajax processing page, then you can link to it in an ajax call.

0
source

Source: https://habr.com/ru/post/1766472/


All Articles