目前,我正在开发一个电子商务网站。我使用while
循环显示产品详细信息。每个产品都有一个带有动态类名和id
的按钮。我想从按钮中获取id
。
这是我的代码:
function get_quote() {
var btn_class = ".get_quote_btn_<?php echo $row['id']; ?>";
var product_id = $(btn_class).attr("id");
var user_id = "<?php echo $user_id; ?>";
var mailSend = $.ajax({
type: 'POST',
url: "http://zti.thecoding.company/get_quote_ajax/",
data: {
product_id: product_id,
user_id: user_id
},
dataType: "text",
success: function(resultData) { //alert("Mail Sended")
}
});
///mailSend.error(function() { alert("Something went wrong"); });
}
<button class="get_quote_btn_<?php echo $row['id']; ?>" onclick="get_quote()" style=" font-size: 14px;padding: 13px;" id="<?php echo $row['id']; ?>">Get Quote</button>
答案 0 :(得分:0)
尝试
onclick="get_quote(this.id)"
并像这样收到get_quote()
function get_quote(id) {
alert(id);
var btn_class = ".get_quote_btn_<?php echo $row['id']; ?>";
var product_id = $(btn_class).attr("id");
var user_id = "<?php echo $user_id; ?>";
var mailSend = $.ajax({
type: 'POST',
url: "http://zti.thecoding.company/get_quote_ajax/",
data: {
product_id: product_id,
user_id: user_id
},
dataType: "text",
success: function(resultData) { //alert("Mail Sended")
}
});
///mailSend.error(function() { alert("Something went wrong"); });
}
答案 1 :(得分:0)
使用以下代码:-
<button class="get_quote_btn" id="<?php echo $row['id']; ?>" onClick="get_quote(this.id)" style=" font-size: 14px;padding: 13px;">Get Quote</button>
function get_quote(id) { //The rest of the code}
这将起作用。
答案 2 :(得分:0)
仅需完成其他答案,代码应如下所示:
function get_quote(product_id) {
var user_id = "<?php echo $user_id; ?>";
var mailSend = $.ajax({
type: 'POST',
url: "http://zti.thecoding.company/get_quote_ajax/",
data: {
product_id: product_id,
user_id: user_id
},
dataType: "text",
success: function(resultData) { //alert("Mail Sended")
}
});
///mailSend.error(function() { alert("Something went wrong"); });
}
按钮:
<button class="get_quote_btn" onclick="get_quote(this.id)" style="font-size: 14px;padding: 13px;" id="<?php echo $row['id']; ?>">Get Quote</button>
很显然,由于get_quote
仅作为函数声明一次,因此<?php echo $row['id']; ?>
将不起作用,因为它不在while循环中