Javascript 如何为“while”循环中的按钮获取动态id属性?
目前我正在开发一个电子商务网站。我使用Javascript 如何为“while”循环中的按钮获取动态id属性?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,目前我正在开发一个电子商务网站。我使用while循环显示产品详细信息。每个产品都有一个带有动态类名和id的按钮。我想从按钮获取id 这是我的密码: 函数get_quote(){ var btn_class=“.get_quote_btn_”; var product_id=$(btn_类).attr(“id”); var user_id=“”; var mailSend=$.ajax({ 键入:“POST”, url:“http://zti.thecoding.company/get_quot
while
循环显示产品详细信息。每个产品都有一个带有动态类名和id
的按钮。我想从按钮获取id
这是我的密码:
函数get_quote(){
var btn_class=“.get_quote_btn_”;
var product_id=$(btn_类).attr(“id”);
var user_id=“”;
var mailSend=$.ajax({
键入:“POST”,
url:“http://zti.thecoding.company/get_quote_ajax/",
数据:{
产品标识:产品标识,
用户id:用户id
},
数据类型:“文本”,
成功:函数(resultData){//警报(“已发送邮件”)
}
});
///error(函数(){alert(“出错”);});
}
试试看
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"); });
}
函数get_quote(id){
警报(id);
var btn_class=“.get_quote_btn_”;
var product_id=$(btn_类).attr(“id”);
var user_id=“”;
var mailSend=$.ajax({
键入:“POST”,
url:“http://zti.thecoding.company/get_quote_ajax/",
数据:{
产品标识:产品标识,
用户id:用户id
},
数据类型:“文本”,
成功:函数(resultData){//警报(“已发送邮件”)
}
});
///error(函数(){alert(“出错”);});
}
使用以下代码:-
<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}
为了完成其他答案,代码应该如下所示:
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"); });
}
函数获取报价(产品id){
var user_id=“”;
var mailSend=$.ajax({
键入:“POST”,
url:“http://zti.thecoding.company/get_quote_ajax/",
数据:{
产品标识:产品标识,
用户id:用户id
},
数据类型:“文本”,
成功:函数(resultData){//警报(“已发送邮件”)
}
});
///error(函数(){alert(“出错”);});
}
和按钮:
<button class="get_quote_btn" onclick="get_quote(this.id)" style="font-size: 14px;padding: 13px;" id="<?php echo $row['id']; ?>">Get Quote</button>
将不起作用,因为它不在while循环中这将仍然保持
未定义,而不使用id
。非常感谢兄弟们帮助您Hanks Sankha ChowdhuryDon不再使用类,使用id,如果您需要更多像user\u id这样的值,请通过函数传递它。