我有一个表单,我正在使用欧芹进行验证:
HTML
<form method="post" action="" id="credirentierform" data-parsley-validate>
<div name="sub_part" style="display: none">
<input id="firstname" name="firstname" required/>
</div>
<div name="sub_part" style="display: none">
<input id="lastname" name="lastname" required/>
</div>
</form>
<a onclick="$('#credirentierform').submit()" href="javascript:;">
JQUERY
$(document).ready(function () {
$('#credirentierform').parsley().subscribe('parsley:form:validated', function (formInstance) {
var searchvalue="Erreur";
$("div[name='sub_part']").each(function() {
if ($(this).html().indexOf("Erreur") >-1){
this.style="";
}
else {
this.style="none";
};
});
});
});
基本上,在提交表单时,Parsley 会添加以下内容:
<li class="parsley-required">Erreur</li>
我的查询代码应该通过 name="sub-part"
遍历每个 div,如果找到 Erreur,则 style="block"
否则 风格=“无”
。但是,它不会那样做。
看来我的代码确实查看了整个页面,而不是在 name="sub-part"
定义的部分内。
我错过了什么? 谢谢
最佳答案
您是否尝试过在第一个结果集中使用 $(this) 以非全局方式查找后续匹配项?
$(this).find("div[name='sub_part']").each(function() {
if ($(this).html().indexOf("Erreur") >-1){
this.style="";
}
else {
this.style="none";
};
});
关于javascript - 欧芹 - 根据 li 的值隐藏/显示 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24885066/