csdn产品小助手 2016-07-07 06:35 采纳率: 0%
浏览 42

parseInt值无错误

I have an AJAX request that does nothing and I don't know why. It doesn't give me any error.my code:

$("#idButton").click(function(){
    $.ajax({
            type: "GET",
            url: "https://api.fixer.io/latest?base=EUR",
            data: {},
            success: function(data) {
                     $("#idUsd").val(parseInt(data['rates']['USD']) * 
                     parseInt($("#idEur").val())); //I think the error is here
            },

            error: function(x, e) { console.log(e); }
    });
});

Can anyone help me?

EDIT:

screenshot

  • 写回答

3条回答 默认 最新

  • weixin_33716941 2016-07-07 06:44
    关注

    Your AJAX request is inside a click handler. I believe you have defined the click handler before you have actually added #idButton to the HTML. You need to make sure that the element already exists when the handler is defined, possibly putting it into $(function() {}) or to use .on() which will register the handler in future element creation as well.

    Case 1:

    You have a script tag inside your head and you define #idButton inside body. In this case this code should solve the problem:

    $(function() {
        $("#idButton").click(function(e) {/*Your handler*/});
    });
    

    Case 2:

    #idButton is created and added at runtime. In that case

    $('body).on('click', '#idButton', function(e) {/*Your handler*/});
    

    should solve the problem. Ideally switch body to the lowest node selector in the tree already existent when you define the handler and which contains the button.

    评论

报告相同问题?