[英]$(this).attr("data-id") returning undefined
I'm building a webshop and on every item there is a button to add that item to the shopping cart.我正在建立一个网上商店,在每件商品上都有一个按钮可以将该商品添加到购物车中。
<?php
$i = 0;
$path = "../images/women/winter/winter1";
$lang = "description_".get_param();
if(!$result = $db->query("SELECT * FROM cloths;")){
die("There was an error running the query [".$db->error."]");
}
while($winter = $result->fetch_assoc()){
echo "<div class = \"boxsale $winter[image]\">
<img src = \"$path.jpg\"/>
<div class = \"test\"> $winter[$lang] </div>
<select>
<option>S</option>
<option>M</option>
<option>L</option>
<option>XL</option>
<option>XXL</option>
</select>
<button class = \"addToCart\" onclick = \"executeJS()\"> <i class = \"fa fa-shopping-cart\" data-id = \"$i\" aria-hidden=\"true\"></i></button>
</div>";
$i++;
}?>
</div>
when you click on the button the executeJS() in the last few lines gets called from a seperate file.当您单击按钮时,最后几行中的 executeJS() 会从单独的文件中调用。
addToCart.js添加到购物车.js
function executeJS(){
var id = $(this).attr("data-id");
$.ajax( {
type: "GET",
url: "ajax.php?id=" + id + "&action=add"
})
.done(function(){
alert("Product has been added.");
});
}
i wanted to grab the data-id from the button that has been clicked, with我想从已单击的按钮中获取数据 ID,
$(this).attr("data-id");
but all i get is a header that has undefined as data-id.但我得到的只是一个未定义为数据 ID 的标头。
id=undefined&action=add
can anyone please point me in the right direction?任何人都可以指出我正确的方向吗?
thanks谢谢
The problem is because $(this)
is not correctly being applied to the element you're trying to access.问题是因为$(this)
没有正确应用于您尝试访问的元素。 When using a string based onclick handler ( onclick="test()"
), this
refers to the window, and NOT the element you're trying to access.当使用基于 onclick 处理程序 ( onclick="test()"
) 的字符串时, this
指的是窗口,而不是您尝试访问的元素。
function test() { console.log(this.id); }
<a href="#" id="test" onclick="test()" data-id="test">Link</a>
If you want to use the string based onclick notation, pass your element in explicitly, and do $(element)
.如果您想使用基于单击符号的字符串,请显式传递您的元素,然后执行$(element)
。
<button class = \"addToCart\" onclick = \"executeJS(this)\" data-id = \"$i\" />
Javascript: Javascript:
function executeJS(element){
var id = $(element).data('id');
...
Personally, skip the attr-id all together, and simply pass your id directly:就个人而言,一起跳过 attr-id,只需直接传递您的 id:
<button class = \"addToCart\" onclick = \"executeJS($id)\" />
It looks like this
may be pointing to the window instead of the button.看起来this
可能指向窗口而不是按钮。
You can fix this by manually looking for the button:您可以通过手动查找按钮来解决此问题:
var id = $(".addToCart").attr("data-id");
This will only really work if you only have one button.只有当您只有一个按钮时,这才真正起作用。 What you should do is use jquery to attach the event to the button.您应该做的是使用 jquery 将事件附加到按钮。
In your javascript:在你的 javascript 中:
$(".addToCart").click(function(e){
var id = $(e.target).attr("data-id");
$.ajax({
type: "GET",
url: "ajax.php?id=" + id + "&action=add"
})
.done(function(){
alert("Product has been added.");
});
})
The e.target refers to the button that is being clicked. e.target 指的是被点击的按钮。
Use regular function ( function(){}
) instead of arrow function:使用常规函数( function(){}
)代替箭头函数:
$(document).on("click",".deleteBtn",function(){
//Your Code
});
Since you're using jQuery, this is how you should be doing it.由于您使用的是 jQuery,因此您应该这样做。 First, remove the inline event handler in your PHP (eg onclick = \\"executeJS()\\"
)首先,删除 PHP 中的内联事件处理程序(例如onclick = \\"executeJS()\\"
)
The other issue is that your buttons don't have a data attribute but their <i>
children do.另一个问题是您的按钮没有 data 属性,但它们的<i>
子级有。
Next, create your jQuery event handler, binding to the button.接下来,创建您的 jQuery 事件处理程序,绑定到按钮。 By doing this you can use $(this).find('i')
to get the fontawesone <i>
element and its data attribute, .attr("data-id")
:通过这样做,您可以使用$(this).find('i')
来获取 fontawesone <i>
元素及其数据属性.attr("data-id")
:
$('button.addToCart').click(function() {
var id = $(this).find('i').attr("data-id");
$.ajax({
type: "GET",
url: "ajax.php?id=" + id + "&action=add"
})
.done(function() {
alert("Product has been added.");
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.