我有一点 jQuery,但把它放在这里是一个简化的版本,以更好地识别任何可能的故障。我在代码中提供了一些注释,以更好地突出我的尝试以及问题。请在代码中找到我的评论。
我的问题是“清除”按钮,清除以前点击的任何输入,而它应该只清除当前活动的输入。闻起来像事件冒泡问题,但我现在无法识别它,因此非常感谢您的帮助。
工作流程是这样的:
我在 .container 中单击 input.trigger (
input[name="a"]
)div#popup 弹出/启动
我单击#popup 内的“清除”按钮,它确实清除了
input[name="a"]
. 我做了一个“还原”来放回值,到目前为止很好,如果有一些默认值,它会还原(这部分不包含在代码中,因为它不是主要问题,如果解决了以下问题)。关闭它。因此输入“a”被清除,但后来通过存储的数据默认值恢复为默认值。目前很好。
我单击另一个 input.trigger (
input[name="b"]
),#popup 启动,更多操作.. 好,但是....我单击“清除”按钮,它会清除输入“a”和“b”,而预计只会清除当前
input.trigger[name="b"]
的输入(而不是以前的输入,“a”)。
这里的解决方案还没有帮助。
$('.container').on('focus', 'input.trigger', function(e) {
e.preventDefault();
// First fix attempt, no use
// https://stackoverflow.com/questions/9153501/how-do-you-stop-children-from-propagating-an-event-triggered-by-a-live-delegate
// Trying to solve problem with event bubbling, but no use
if (e.target != this){
return true;
}
var input = $(this);
// somewhere added a class focused on input focused, please ignore.
// we add blur here to make any button or input inside popup clickable.
$('input:not(.focused)').blur();
// Added dynamic data("popup") based on input name (a, b, c, d)
$("#popup").data("popup", this.name).css({
left: "20px",
top: input.offset().top + 24 + "px"
})
// Second fix attempt, no use
.stop(true, true)
.show('slow', function () {
var currentPopup = $(this),
popupName = currentPopup.data("popup"),
// Trying to get input/trigger name from data "popup" per clicked input name.
// I tried putting this after `var input = $(this);` above but put it here later
// thinking it should solve the problem, but alas not.
theinput = $('input[name="' + popupName + '"]'),
// Used for other dynamic interaction with some classes (.a_popup, .b_popup, etc).
popupid = theinput.data('popupid');
currentPopup.on('click', '.clear', function(e) {
// Third fix attempt, no use
e.stopPropagation();
// The problem is here:
// The button clears out any previous (opened) input.trigger,
// while it should only clears the input with name == current popupName
// Why this also bubbles to previously opened input.trigger "popupid"
console.log(popupid);
theinput.val("");
});
// More buttons here: Revert and Close. But once the "Clear" button issue is solved, this is no issue. So excluded.
});
})
.on('blur', 'input.trigger', function (e) {
e.preventDefault(); // no use
var hidden = $("#popup"),
popupName = this.name;
if (hidden.data("popup") === popupName) {
hidden.hide().removeData("popup");
}
});
HTML:
<body>
<div id="page-wrapper">
<div class="container">
<form>
<!-- This input value is dynamically changed via other function -->
<input class="trigger" name="a" value="one" data-popupid=".a_popup" data-default="a_value">
<input class="trigger" name="b" value="" data-popupid=".b_popup" data-default="">
<input class="trigger" name="c" value="three" data-popupid=".c_popup" data-default="c_value">
<input class="trigger" name="d" value="four" data-popupid=".d_popup" data-default="d_value">
<form>
<!-- Ignore below divs, but here to have a general idea with above data-popid
This is not directly addressed in the code above -->
<div class="a_popup">Content</div>
<div class="b_popup">Content</div>
<div class="c_popup">Content</div>
<div class="d_popup">Content</div>
</div>
</div><!-- page wrapper -->
<!-- This popup has data("popup") namee by above inputs (a, b, c, d) assigned dynamically -->
<div id="popup">
<!-- Sometext and additional inputs -->
<button class="revert">Revert</button>
<button class="clear">Clear</button>
<button class="close">Close</button>
</div>
</body>
CSS:
#popup {
background: #fff;
display: none;
height: 200px;
width: 200px;
z-index: 99999;
position: absolute;
}
我希望我能说清楚,但如果不是,我会更新上面简化代码中遗漏的任何内容。感谢您发现实际问题的任何提示。
更新:
$('.container').off('input.trigger');
附加到关闭按钮。