douzhan1031 2015-02-05 14:56
浏览 385
已采纳

按钮onclick ajax调用

I am trying to put together a webpage that has multiply pages in 1 file which checks the session that i've created to show a page according to the session value.
However, i want to create a button that does a ajax call on the background and returns the results thought JSON, BUT i don't know if i'm doing it the right way.
I have this js:

$(document).ready(function() {
var alert = $('.Cmessage'); // DIV TO PUSH RESULTS TO

$("#terug").on('click', function(){

$.ajax({
  url: 'http://www.XXX.nl/shop/offerte/back.php', // form action url
  type: 'get', // form submit method get/post
  dataType: 'json', // request type html/json/xml 
  beforeSend: function() {
    alert.fadeOut();
  },
success: function(result) {
    if(result.error){
        alert.html(result.html).fadeIn();
        console.log(e)
    }else{
        alert.html(result.html).fadeIn();
        }
    }
});
});
});

This js needs to check whenever i click on:

<button type="button" class="terug">Open/Laden</button>

But somehow i fail to get it attached to the class "terug" and do something with it.

Does anybody has any suggestions/idea's on how i can do this better and get it working ? :P

Greetings.

  • 写回答

5条回答 默认 最新

  • doufutao4428 2015-02-05 15:03
    关注

    You are selecting your button with the wrong jQuery selector.

    $("#terug").click..

    Try to use $('.terug') instead.

    '#' selecting by id
    '.' selecting by class

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退