我有這個代碼:
<div id="product_<?php echo $product->id; ?>"></div>
這是ajax js代碼:
$(function () {
$('.expand').on('click', function (e) {
e.preventDefault();
var token = "<?php echo $this->security->get_csrf_token_name(); ?>";
var hash = "<?php echo $this->security->get_csrf_hash(); ?>";
var productID = $(this).data("id");
$.ajax({
type: 'GET',
dataType: 'html',
url: 'marketplaces/marketplaces/test_expand',
data: {
product_id: productID,
token: hash
},
beforeSend: function () {
},
success: function (data, textStatus) {
$("#product_" productID).html(data);
},
error: function (xhr, textStatus, errorThrown) {
alert('request failed');
},
complete: function () {},
});
});
});
還有這個按鈕:
<div data-id="<?php echo $product->id; ?>" class="expand">
<a href="#" class="bt btn-primary btn-sm">Offers
<i class="fas fa-chevron-down"></i>
</a>
</div>
當我單擊按鈕 div 時,id=product_(ID)它會顯示來自 ajax 的資料。它正在作業!當我再次單擊按鈕時,我想隱藏這個 div!
怎么可能?謝謝!
uj5u.com熱心網友回復:
您可以使用自定義代碼放置條件以顯示隱藏功能。
這是示例代碼:
$(function () {
$('.expand').on('click', function (e) {
e.preventDefault();
var currBoxObj = this;
var token="<?php echo $this->security->get_csrf_token_name(); ?>";
var hash="<?php echo $this->security->get_csrf_hash(); ?>";
var productID = $(this).data("id");
if(!$(currBoxObj).hasClass("showingblock")){
$.ajax({
type: 'GET',
dataType: 'html',
url: 'marketplaces/marketplaces/test_expand',
data:
{
product_id: productID,
token: hash
},
beforeSend: function () {
},
success: function (data, textStatus) {
$(currBoxObj).addClass("showingblock");
$("#product_" productID).show();
$("#product_" productID).html(data);
},
error: function (xhr, textStatus, errorThrown) {
alert('request failed');
},
complete: function () {
},
});
}else{
$("#product_" productID).hide();
$(currBoxObj).removeClass("showingblock");
}
});
});
uj5u.com熱心網友回復:
$('[data-id=product_ID]').hide();
我認為您可以像這樣使用此代碼。希望對你有幫助。
uj5u.com熱心網友回復:
您應該在打開元素時為其添加一個類
$("#product_" productID).addClass('is-open')
然后在第二次點擊時檢查它是否有類(在 ajax 呼叫之上)
if ($("#product_" productID).hasClass('is-open')) {
$("#product_" productID).hide().removeClass('is-open')
return
}
如果你想在打開一個之前關閉所有,那么它很簡單:
$('.is-open').hide().removeClass('is-open')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/387779.html
標籤:javascript 查询 阿贾克斯
下一篇:包含庫的最佳方法是什么?
