在许多字段集中,我使用下一步按钮尝试查找是否有“#required” id 输入,然后,如果它为空,则返回 false(留在此字段集中),否则继续操作...
if ($(this).attr('required').length == 0) {
alert ('oui');
if (!$(this).attr('required'))
return false;
}
else
alert ('non');
不过$(this).attr('required').length
是undefined
因为没id
找到。
需要帮助,谢谢大家。
我解释:在字段集(动态创建)中,如果存在 id #required 的输入,我必须检查它是否为空。如果是,单击按钮将返回 false。我是法国人,也是 jQuery 新手,所以对一切感到抱歉 =\
的HTML:
[...]
<fieldset>
<h2 class="fs-title">Accueil Particulier</h2>
<h3 class="fs-subtitle">Nombre de personnes :</h3>
<input type="number" class="required" name="par_venu_f" placeholder="Femme(s)"/>
<input type="number" class="required" name="par_venu_h" placeholder="Homme(s)"/>
<br/><br/><input type="button" name="previous" class="previous action-button" value="Précédent"/>
<input type="button" name="next" class="next action-button" value="Suivant"/>
</fieldset>
[...]
编辑 :
好的,现在我在这里:
if ($(this).parent().children().hasClass('required')) {
if ($(this).parent().children('.required').val().length == 0) {
alert('find, empty')
return false;
};
alert ('find, full');
}
else alert ('not find');
没关系,但是当它只检查第一个输入 [type=text] 时,我如何检查其他输入?
编辑 2:我尝试了 .each() 函数,但不明白它是如何工作的......
回答 :
非常感谢@TrueBlueAussie,即使我在几分钟前发现了这个:
//check if class="required" exist
if ($(this).parent().children().hasClass('required')) {
//start of each class="required"
$(this).parent().children('.required').each(function() {
//if empty, do not continue
if ($(this).val().length == 0) {fill_required = false;};
//end each
});
// end if exist
};
// if there is one empty field, will be false so ...
if (!fill_required) {
// make it true before leave then ...
fill_required = true;
// leave.
return false;
//end if one empty
};