我想写JQuery,它会发现页面上是否存在Class =“Mandatory”,如果有任何元素有这个类,那么只执行某些操作。
谢谢
解决方法
当您使用jQuery搜索时,请检查您打了多少元素
if ($(".Mandatory").length > 0) {
// Do stuff with $(".Mandatory")
$(".Mandatory").each(function() {
// "this" points to current item in looping through all elements with
// class="Mandatory"
$(this).doSomejQueryWithElement();
});
}
编辑
如果你想做这个检查你的提交按钮点击,只需点击后做那个检查:
$("input[type='submit']").click(function() {
if ($(".Mandatory").length > 0) {
// Do some code here
}
});