显示和隐藏 div 内容 ajax
Show and hide div with ajax content
我有这个代码:
<div id="product_<?php echo $product->id; ?>"></div>
这是ajaxjs代码:
$(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>
当我用 id=product_(ID)
单击按钮 div 时,它会显示来自 ajax 的数据。它的工作!我想在再次单击按钮时隐藏此 div!
怎么可能?谢谢!
$('[data-id=product_ID]').hide();
我想你可以像这段代码一样使用。
希望对你有所帮助。
您可以为显示隐藏功能添加自定义代码条件。
示例代码如下:
$(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");
}
});
});
你应该在打开元素时添加一个class
$("#product_" + productID).addClass('is-open')
然后在第二次点击时检查它是否有 class(在 ajax 调用上方)
if ($("#product_" + productID).hasClass('is-open')) {
$("#product_" + productID).hide().removeClass('is-open')
return
}
如果你想在打开一个之前关闭所有,那么这很简单:
$('.is-open').hide().removeClass('is-open')
我有这个代码:
<div id="product_<?php echo $product->id; ?>"></div>
这是ajaxjs代码:
$(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>
当我用 id=product_(ID)
单击按钮 div 时,它会显示来自 ajax 的数据。它的工作!我想在再次单击按钮时隐藏此 div!
怎么可能?谢谢!
$('[data-id=product_ID]').hide();
我想你可以像这段代码一样使用。 希望对你有所帮助。
您可以为显示隐藏功能添加自定义代码条件。
示例代码如下:
$(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");
}
});
});
你应该在打开元素时添加一个class
$("#product_" + productID).addClass('is-open')
然后在第二次点击时检查它是否有 class(在 ajax 调用上方)
if ($("#product_" + productID).hasClass('is-open')) {
$("#product_" + productID).hide().removeClass('is-open')
return
}
如果你想在打开一个之前关闭所有,那么这很简单:
$('.is-open').hide().removeClass('is-open')