Assigning HTML returned via ajax to any HTML element value

陌路散爱 提交于 2019-12-11 14:08:10

问题


I have trying to assign the html returned from ajax to the value of any html element. The html returned only contains 22.5. So I want to assign this value to any html element value.

The code I am using is:

$(document).ready(function(){
    $("#coupon_btn").click(function(){
        //alert("hello");
        var coupon=$("#coupon").val();
        var mem_price=$("#mem_price").val();
        $.ajax({
           type: 'POST',
           url: 'http://localhost/freakinout1/paypal/coupon.php',
           data: "coupon="+coupon+"&price="+mem_price,
           cache: false,
           success: function(html) {
                var pay = $("#Payment_Amount").val();
                $("#Payment_Amount").val() = html;
                alert(pay);
               //$('#search-results-container').html(html);
           }
        });
    });
});

The error I am getting in error console is invalid assignment left hand side.

So could someone suggest me what I am doing wrong?


回答1:


The val() function accepts a parameter to set the value of the object on which it is called, pass html variable to val() Note val() is used with html elements of input type like text, checkbox etc. If Payment_Amount is div or span then you will need to use html() or text().

Change

$("#Payment_Amount").val() = html;

To

$("#Payment_Amount").val( html);



回答2:


for setting a value you need to pass string in val()..

try this

$("#Payment_Amount").val() = html;

replace with

$("#Payment_Amount").val(html);
                    //---^^^^ here


来源:https://stackoverflow.com/questions/15385435/assigning-html-returned-via-ajax-to-any-html-element-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!