如何找到因 .live(jQuery 方法)而导致事件的元素的 ID?
以下是一个示例:
$("form[data-f=true]").live("submit", function (evt) {
/*
If I have <input type="submit" name="Submit1" data-f="true" /> and this button is clicked, then I want to know it's Submit1 button.
*/
});
我检查了 evt 及其属性/字段,但没有运气。
最佳答案
使用这个,
event.target //DOM object.
evt.target.id //id of source element.
sourceElement = $(evt.target.id); // will get you jQuery object
注意,从 jQuery 1.7 开始,$.live 已被弃用。使用 $.on 代替。
获取导致提交的元素。
$(document).ready(function() {
$(document).on('submit', 'form[data-f=true]', function() {
var elem = $("input[type=submit][clicked=true]");
alert(elem.attr('id'));
// DO WORK
});
$("form[data-f=true] input[type=submit]").click(function() {
$("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
$(this).attr("clicked", "true");
});
关于jquery - 如何查找由于jquery.live而导致事件的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13955541/