我有一个jQuery对话框,允许用户添加和删除选项(使用jQuery来更新DOM)。每次选项被添加,输入和一个按钮(“删除”)添加到对话框中的格式如下 -查找previoius元素与某个类
<div id="existing-options">
<div class="dialog-option">
<div class="dialog-right">
<div class="remove_option button-secondary" id="remove_new_0">Delete</div>
</div>
<div class="dialog-left">
<input type="text" value="1" class="option text ui-widget-content ui-corner-all" id="new_0" name="new_0">
</div>
</div>
</div>
我试图让这个如果用户按下回车时,他们都集中在一个输入上,其相应的'Delete'按钮的click
事件被触发。
我已经做了一些研究,.sibling()
似乎不合适。 .prev()
看起来像我最好的选择,但那是行不通的。有人可以告诉我最好的方法来实现这个工作吗?
$(document).ready(function(){
dialog_options_container = $('#existing-options'); // The container which holds the option inputs/buttons
/** Capture the 'enter' key when removing an option */
dialog_options_container.bind('keyup', '.option', function(e){
if(e.keyCode === 13){
$(this).prev('.remove_option').click();
return false; // Ignore the default event
}
});
});
在你的JavaScript里是什么意思't.dialog_options_container'? – Epsil0neR
您可以简单地使回车键具有一个功能,就像删除按钮具有绑定功能一样。所以说你点击的事件是.remove()父母只要做(如果这样)(e.which === 13){$(this).parent()。remove(); }只是一个小例子,因为我在我的手机上哈哈。 – EasyBB
@ Epsil0Ner - 我已经更新了这个问题来证明这一点。 –