我有一个像这样的 HTML 标记。
<div class="formSection custom">
<form id="support">
</form>
<div>
div 的类名“custom”是动态添加的,我需要检查表单提交。
到目前为止,我已经有了这个,但它不起作用。
$('#support').on('submit', function(e) {
if ($(".support").parent().hasClass("custom")) {
alert("Yeah, It does have class");
}
}
当我这样做时它会起作用
if ($(".formSection").hasClass("custom"))
我不希望我的jquery 如此死板。我想追踪表单的父级并检查它的类。有什么办法可以做到吗?
最佳答案
首先,您正在调用
if ($('.support'))
表示您正在搜索类为 .support
的元素,但是你的<form>
id 为 #support
可能最好的方法是 - 注意 $(this)
的使用
$('#support').on('submit', function(e) {
if ($(this).parent().hasClass('custom')) {
alert('Yeah, It does have class');
}
}
关于javascript - jQuery:在提交时检查表单父 div 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29498624/