2015-04-07 19 views
0

我有这样的HTML标记。jQuery:提交时检查表单的父级div'sclass

<div class="formSection custom"> 
    <form id="support"> 
    </form> 
<div> 

类名“定制”到div动态添加,我需要检查表单提交。

到目前为止,我有这个,它不工作。

$('#support').on('submit', function(e) { 
     if ($(".support").parent().hasClass("custom")) { 
      alert("Yeah, It does have class"); 
     } 
} 

它的工作原理,当我做

if ($(".formSection").hasClass("custom")) 

我不希望我的jQuery是那么死板。我想追查表格的父母并检查其班级。有没有办法我可以做到这一点?

回答

1

首先,您呼叫

if ($('.support')) 

说明你有.support正在寻找一个元素,但你的<form>ID的#support

可能是最好的办法到这样做会 - 注意使用$(this)

$('#support').on('submit', function(e) { 
    if ($(this).parent().hasClass('custom')) { 
     alert('Yeah, It does have class'); 
    } 
}