我正在尝试通过触发click事件发送AJAX帖子,click事件有效,但AJAX请求无效。
private async Task ManageFulfillments(long orderId, Order order)
{
if (order.Fulfillments == null || !order.Fulfillments.Any()) return;
var newFulfillmentIds = order.Fulfillments.Select(c => c.ExternalFulfillmentId).ToList();
var dbFulfillments = await _fulfillmentRepository.GetAll().IgnoreQueryFilters()
.Where(c => c.OrderId == orderId)
.Select(c => new { c.Id, c.ExternalFulfillmentId }).ToListAsync();
var dbFulfillmentIds = dbFulfillments.Select(c => c.ExternalFulfillmentId).ToList();
// Delete Fulfillments that are not present in new Fulfillments list
var deletedFulfillments = dbFulfillmentIds.Except(newFulfillmentIds).ToList();
if (deletedFulfillments.Any())
{
await _fulfillmentRepository.DeleteAsync(c =>
deletedFulfillments.Contains(c.ExternalFulfillmentId) && c.ExternalOrderId == orderId);
}
// Update existing Fulfillments ids
order.Fulfillments
.Where(c => dbFulfillmentIds.Contains(c.ExternalFulfillmentId))
.ToList()
.ForEach(async c =>
{
c.Id = dbFulfillments.Where(p => p.ExternalFulfillmentId == c.ExternalFulfillmentId)
.Select(p => p.Id).FirstOrDefault();
await _fulfillmentRepository.UpdateAsync(c);
});
// New Fulfillments will automatically be added by EF
}
如果我手动单击$(this).find(".headermodelbtn"+text).trigger( "click" );
$(".headermodelbtn"+text).click(function() {
alert(text);
$.ajax({
url: siteurl + "/audit/ajax/audit_setup.php",
type: "POST",
data: { text: text, action: 'get_child_attribute' },
success: function (data) {
var obj = $.parseJSON(data);
$(".appendchildoptions").html(obj.list);
}
});
,它将起作用。
答案 0 :(得分:0)
请尝试:
$(".headermodelbtn"+text).trigger( "click" )
代替:
$(this).find(".headermodelbtn"+text).trigger( "click" );
并确保在 click事件绑定后运行该代码。
编辑: 查看以下控制台打印信息( ,假设id为buttonInput1的html dom元素与具有class = test 的dom元素相同):
使用devtools元素$(“#buttonInput1”)打印,我们只有一个文档上下文,(上下文:document)-如下所示:
jQuery.fn.init {context: document, selector: "#buttonInput1"}
并使用devtools元素$(“。test”)打印,这里有一个集合(请注意“ [input#button .....]”)-如下所示: < / p>
jQuery.fn.init [input#buttoninput1.test, prevObject: jQuery.fn.init(1), context: document, selector: ".test"]
所以,它们不一样。如果按类查找,则可能需要在注册触发器之前减少更多设置。请尝试使用唯一的ID,例如:
$(“#headermodelbtn1”)。trigger(“ click”)