问题
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