javascript - jQuery:在提交时检查表单父 div 的类

标签 javascript jquery html

我有一个像这样的 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/

相关文章:

javascript - 使用 css 内容循环 javascript

javascript - 查找损坏的 Creep(或如何调试)

javascript - 将超时设置为无穷大时遇到问题

javascript - react js eventListeners 没有按预期删除

javascript - 如何使用 javascript 为 webkit 滚动条编写 css?

css - 页面底部的div,下面有图片

javascript - 如何根据 div 当前的类别更新点击功能?

php - 从 .post 检索数据

javascript - 使用 jQuery 获取没有父元素的 DOM 元素的内容

html - 输入字段的边距不会从 div 顶部获得 0px