0

The below URL works fine:

index.php?page=search_result&maker=Any

I want to get into this URL from another page using Jquery Ajax function. Here is my Ajax call:

$(function(){

    $(".by_maker").on('click',function(){

        var c_maker = $(this).attr('href');

         $.ajax({
               type: "GET",
               datatype: "html",
               url: "index.php?page=search_result",
               data: "maker="+c_maker,
               success: function(response){
                       html(response);} 
           });
    });
});

I like to get the value of 'maker' from the href like below html:

<a class="by_maker" href="toyota">Toyota</a>

NB: there is not 'form' and 'input' elements. If I click on the Toyota link its not going to the desired URL!!! So, what am I doing wrong??? Any help is appreciated. Thanks in advance.

4

3 回答 3

1

添加return:false;到事件处理程序以停止默认操作。

$(".by_maker").on('click',function(){

    var c_maker = $(this).attr('href');

     $.ajax({
           type: "GET",
           datatype: "html",
           url: "index.php?page=search_result",
           data: "maker="+c_maker,
           success: function(response){
                   html(response);} 
       });
       return false;
});

或者,您可以将 传递event给回调函数,然后preventDefault();

$(".by_maker").on('click',function(event){
    event.preventDefault();
于 2013-08-23T08:26:37.650 回答
0

请使用防止默认值,这样它就不会被重定向到任何地方。或者只是从


    $(".by_maker").on('click',function(e){
        e.preventDefault();
        var c_maker = $(this).attr('href');

         $.ajax({
               type: "GET",
               datatype: "html",
               url: "index.php?page=search_result",
               data: "maker="+c_maker,
               success: function(response){
                       html(response);} 
           });
    });

或者

保持 jquery click 和现在一样 <a class="by_maker">Toyota</a>

于 2013-08-23T08:30:29.553 回答
0

您还可以使用 event.preventDefault() 来阻止默认操作:

    $(function(){
    $(".by_maker").on('click',function(event){
    event.preventDefault()
    var c_maker = $(this).attr('href');

     $.ajax({
           type: "GET",
           datatype: "html",
           url: "index.php?page=search_result",
           data: "maker="+c_maker,
           success: function(response){
                   html(response);} 
       });
     });
   });
于 2013-08-23T08:33:32.640 回答