简体   繁体   中英

Variable not working in simple Ajax post

Can't seem to get the variable getID to work. I'm trying to change the html of the div. I know that the variable has the right value.

$('.cardid').change(function() {
    var getID = $(this).attr('value');

        $.ajax({
        type: "POST",
        url: "inc/change_thumbnail.php",
        data: "id="+getID,
        cache: false,
        success: function(data) {
            $("#"+getID).html(data);
            alert("success");
        },
        error: function (err) {
            alert("error"); 
        }
    });

});        

将$ .ajax中的数据作为数据:{id:getID}而不是数据:“ id =” + getID,

Use val to get the value of an input :

var getID = $(this).val();

As you're making a POST request, you should also use the data argument to let jQuery properly send the value :

$.ajax({
    type: "POST",
    url: "inc/change_thumbnail.php",
    data: {id:getID},
    cache: false,
    success: function(data) {
        $("#"+getID).html(data);
        alert("success");
    },
    error: function (err) {
        alert("error"); 
    }
});

You can try this:

$('[id="'+getID+'"]').html(data);

and yes you should pass it this way:

data:{id:getID}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM