jQuery选择问题,事件发生时如何选择元素?
我有几个按钮,根据数据库中存储的状态true或false单击它们,0:使用ajax更改数据库中的值,1:显示确认模式,如果用户确认,则再次使用ajax更改值 HTML: 所以,在第一条语句中,当更改数据状态时,选择器$this起作用。但当出现模态时,我如何选择元素,这是最初采用的:.sbs nwlttr、.sbs evt、.sbs prodjQuery选择问题,事件发生时如何选择元素?,jquery,Jquery,我有几个按钮,根据数据库中存储的状态true或false单击它们,0:使用ajax更改数据库中的值,1:显示确认模式,如果用户确认,则再次使用ajax更改值 HTML: 所以,在第一条语句中,当更改数据状态时,选择器$this起作用。但当出现模态时,我如何选择元素,这是最初采用的:.sbs nwlttr、.sbs evt、.sbs prod 感谢您的关注。首先,尽管我怀疑您仅将其用于测试,但请避免使用一位数的类名,以及以数字开头的类名,因为在css中不允许这样做。可以在中找到更多详细信息,例如,
感谢您的关注。首先,尽管我怀疑您仅将其用于测试,但请避免使用一位数的类名,以及以数字开头的类名,因为在css中不允许这样做。可以在中找到更多详细信息,例如,将class=1重命名为class=1 另一方面,唯一的问题是,button变量不是全局变量,只在其中一个函数中声明。在外部声明它,然后您也可以在其他函数中使用它,如下所示:
var button;
$('.one, .two, .three').click(function() {
button = $(this);
// ...
}
confirm_modal.find('#confirm').click(function() {
alert(button.text()); // for example
}
我设置了一个提琴来演示这一点,与上面的稍有不同,但应该清楚地显示其行为:为什么不使用已经为您存储了$this的按钮变量…?因为变量按钮在if语句中初始化,如果单击模式按钮,则不确定,但它看起来像是确认模式。查找“确认”。clickfunction在$”.sbs nwlttr、.sbs evt、.sbs prod.clickfunction中,并且它没有在结尾处关闭不}这是故意的吗?我看到你刚刚编辑了你的原始帖子,将选择器转换为数字,但这不是有效的标记,如果我是你的话,我会把它们改回去——这里有更多的内容:这是我的错误,模态单击的事件在单击跨度元素之外
var confirm_modal = $('#unsubscribe-confirmation');
$('.1, .2, .3').click(function() {
var button = $(this);
var state = $(this).data('state');
var type = $(this).data('type');
if ( state == 1 ) {
confirm_modal.modal();
} else {
$.ajax({
'url': get_base_url_with_ajax_url() + 'subscribe',
'data': { "state": state, "type" : type },
'type': 'POST',
'dataType': 'json',
success: function(result) {
button.attr('data-state', '1');
}
});
}
});
confirm_modal.find('#confirm').click(function() {
$.ajax({
'url': get_base_url_with_ajax_url() + 'subscribe',
'data': { "state": state, "type": type },
'type': 'POST',
'dataType': 'json',
success: function(result) {
$(this).attr('data-state', '0');
confirm_modal.modal('hide');
}
});
var button;
$('.one, .two, .three').click(function() {
button = $(this);
// ...
}
confirm_modal.find('#confirm').click(function() {
alert(button.text()); // for example
}