[英]jQuery event binding on dynamically created DOM elements
Sorry about the ambiguous title but I couldn't think of anything better. 抱歉,标题含糊不清,但我想不出更好的办法。
I've been struggling with this issue for a few days and haven't been able to find a way around it. 我已经为这个问题苦苦挣扎了几天,却一直没有找到解决方法。 A little background about my code: I have a textbox savedInfo
and it stores values from different sessions. 关于我的代码的一些背景知识:我有一个文本框savedInfo
,它存储了来自不同会话的值。 So if savedInfo
is empty, I'd like to confront the user with another textbox
. 因此,如果savedInfo
为空,我想用另一个textbox
来面对用户。 Once the user inputs info into the textbox
and presses enter
then the .val()
of the textbox
goes to savedInfo
and the textbox
is replaced with a label that has 2 buttons that appear when hovered. 一旦用户输入信息到textbox
和按压enter
然后.val()
的textbox
转到savedInfo
和textbox
被替换为具有盘旋时出现的2个按钮的标签。 These two buttons give the user the ability to remove or save the textbox
. 这两个按钮使用户可以删除或保存textbox
。
The problem I am having is that my code doesn't seem to flow correctly. 我遇到的问题是我的代码似乎无法正确流动。 For instance, I have an if statement that checks if savedInfo
is empty and if it isn't then show the label text of savedInfo
. 例如,我有一个if语句,用于检查savedInfo
是否为空,如果不为空,则显示savedInfo
的标签文本。 Then INSIDE the keypress
function I check if one of the hover buttons is pressed but the code doesn't work since it is inside the keypress
function. 然后在keypress
功能内部,我检查是否有一个悬停按钮被按下,但是代码不起作用,因为它在keypress
功能内部。 If I take it out of the keypress
function then it doesn't work for when the keypress
label appears. 如果我将其从keypress
功能中keypress
则当keypress
标签出现时,它将无法正常工作。
Probably best if I explain with pseudo code: 如果我用伪代码解释可能最好:
$(document).ready(function() {
if($("savedInfo").val() != "") {
someDiv..innerHTML(<label>$("savedInfo").val() + hover shows buttons</label>);
} else {
//Create textbox id `textbox`
}
$(function() {
$("#textbox").keypress(function(e) {
if (e.keyCode == 13) {
//set innerHTML to a label with text of `textbox`.
//Check if button#1 is pressed. If yes, then do something
//Check if button#2 is pressed. If yes, then do something
}
});
});
});
So the button#1 and button#2 ids
are being created inside the if but don't work inside the if because they are inside the keypress. 因此,button#1和button#2 ids
是在if中创建的,但是在if中则无法使用,因为它们在按键内部。
I hope this wasn't too confusing. 我希望这不会太令人困惑。 Any ideas on how to make this work better? 关于如何使这项工作更好的任何想法? Thanks 谢谢
Here's a working example with jQuery based off of your requirements. 这是一个基于您的需求的jQuery工作示例。
I really don't understand what the use case is here though and it begs a few questions about reuse of the original text-box, whether this is going to occur multiple times on a page or not, why you wouldn't just show the buttons right away (vs assuming the user knows that they must press enter), and whether this could be better solved using templates? 我真的不明白用例是什么,它引起了一些有关原始文本框重用的问题,无论这是否会在页面上多次出现,为什么您不只显示按钮(假设用户知道他们必须按Enter键),是否可以使用模板更好地解决此问题? Seems that this is more difficult than it should be for such a simple interaction. 似乎这比进行这种简单的交互要困难得多。
Anyhow, I hope this helps move you forward. 无论如何,我希望这能帮助您前进。
(function () { function promptForInfo(elem) { var html = "<div id='newInfoContainer'><label>New info: <input id='newInfo' type='text' placeholder='Please provide info...' /></label></div>"; $(elem).parent().append(html); $("#newInfo") .focus() .on("keydown", function (e) { if (e.keyCode === 13) { $(elem).val(this.value); $(this).parent().remove(); var buttonsHtml = "<div id='buttonContainer'><input class='saveButton' type='button' value='Save' /><input class='removeButton' type='button' value='Remove' /></div>"; $(elem).parent().append(buttonsHtml); $(".saveButton").on("click", function (e) { saveInfo(e); }); $(".removeButton").on("click", function (e) { removeInfo(e); }); } }); } function saveInfo(e) { $(e.target.parentElement).remove(); $("body").append("<p>Saved</p>"); } function removeInfo(e) { $(e.target.parentElement).remove(); $("#savedInfo").val(""); $("body").append("<p>Removed</p>"); } function inspectSavedInfo() { if (!$("#savedInfo").val()) { promptForInfo($("#savedInfo")); } } $(document).ready(function () { // Inspect on any change after init $("#savedInfo").on("change", function () { inspectSavedInfo(); }); // Trigger Inspection of savedInfo on init inspectSavedInfo(); }); }());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="savedInfoContainer"> <label>Saved Info: <input id="savedInfo" type="text" value="" /></label> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.