我正在使用 ajax 來顯示產品資料,然后在使用產品 id 時單擊特定的洗掉,但無法獲得產品 id,因為它顯示未定義,你們能提出一些建議嗎?
在此代碼中,我首先獲取購物車下拉選單的產品詳細資訊,然后通過第一個 ajax 使用洗掉按鈕以串列格式顯示它,然后單擊洗掉應該使用我擁有的元素產品 ID 洗掉第二個 ajax 中的元素保存在 data-r 中,但在控制臺上,它顯示 undefined 誰能告訴我是什么問題。據我所知,元素是動態創建的,但我沒有得到解決方案
function TopCartVal() {
// Used Route inside the ajax
var url = "{{ route('pdt.details', ':id') }}";
var yui = "{{ route('delete_fr.cart', ':id') }}";
// Ajax Structure
$.getJSON( "{{ route('my.cart','json') }}", function( data ) {
console.log(data);
var Cart = $('#cart_dp_sec_pdt_desc');
// Loop for data
$.each(data, function(key , value) {
console.log(value);
url = url.replace(':id', value.product_id);
yui = yui.replace(':id', value.product_id);
Cart.append(`
<div >
<div >
<div >
<a href="` url `">
<img src="{{ asset('')}}` value.image `" alt="從使用 jQuery 的 Ajax GET 回應中的選定 HTML 元素獲取 id 的問題"/>
</a>
</div>
</div>
<div >
<h3 >
<a href="` url `">` value.product_name `</a>
</h3>
<div >` value.currency value.product_price `</div>
</div>
<div >
<a href="#"
data-r"` value.id `"
>
<i ></i>
</a>
</div>
</div>
`);
});
});
}
// delete part
$(document).on('click', '.cart_dp_btn_ctn_delete', function(e){
e.preventDefault();
var id = $(this).attr('data-r'); // id of to be deleted element
var yui = "{{ route('delete_fr.cart', ':id') }}";
yui = yui.replace(':id', id);
console.log(yui);
console.log(id); // it is showing undefined`enter code here`
// $.getJSON( yui, function(data){
// TopCartVal();
// });
});
uj5u.com熱心網友回復:
你在=這里缺少一個:
data-r"` value.product_id `"
但是正確使用模板文字會更容易看到
`<div >
<div >
<div >
<a href="${url">
<img src="{{ asset('')}}${value.image}" alt="從使用 jQuery 的 Ajax GET 回應中的選定 HTML 元素獲取 id 的問題"/>
</a>
</div>
</div>
<div hljs-number">7">
<h3 hljs-string">">
<a href="${url}">${value.product_name}</a>
</h3>
<div hljs-string">">${value.currency value.product_price}</div>
</div>
<div hljs-number">1 action">
<a href="#" data-r="${value.product_id}" hljs-string">">
<i hljs-string">"></i>
</a>
</div>
</div>`
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/347671.html
標籤:javascript 查询 阿贾克斯
