好了,伙计们,我有下面的结构,10次,这意味着10种形式。如您所知,$('button')
将处理任何按钮的事件。
<div>
<form ...>
<div><button type='button'>Submit</button></div>
<div><input /></div>
<div><input /></div>
<div><input /></div>
<div><input /></div>
</form>
</div>
仅使用JQuery及其遍历方法,如何在单击按钮时从$(this)
获取<form>
??
我之前混合使用了parent(),prev()等,但是没有得到它。例如,$(this).closest('form').nodeName
在警报弹出窗口中显示"undefined“。
发布于 2012-11-15 19:43:41
使用
$(':button').closest('form')
更新
$(':button').click(function(){
alert($(this).closest('form').attr('class'));
});
发布于 2012-11-15 19:55:50
这肯定会起作用:
$('button').click(function(){
alert($(this).closest('form').attr('id'));
});
发布于 2012-11-15 19:44:22
你试过了吗?
$('button').parents('form:first').get(0).nodeName;
像这样
$('button').click(function(){
alert($(this).parents('form:first').get(0).nodeName);
});
DEMO
更新:
你可以像这样简单地submit
它
$('button').click(function(){
$(this).parents('form:first').get(0).submit();
});
https://stackoverflow.com/questions/13396790
复制相似问题