Javascript jQuery表单验证问题,其他框可见时
我有下面的“其他”框。我想验证该框,使其仅在可见时不为空。对于应该直截了当的事情,我遇到了麻烦。非常感谢您的帮助。谢谢Javascript jQuery表单验证问题,其他框可见时,javascript,jquery,validation,Javascript,Jquery,Validation,我有下面的“其他”框。我想验证该框,使其仅在可见时不为空。对于应该直截了当的事情,我遇到了麻烦。非常感谢您的帮助。谢谢 else if($('#myBox_' + id + ':visible')) { if(!blank('otherprodtypebox[<xsl:value-of select="@id"/>]')){alert("Please enter a valid other option");return false;} } <script
else if($('#myBox_' + id + ':visible')) { if(!blank('otherprodtypebox[<xsl:value-of select="@id"/>]')){alert("Please enter a valid other option");return false;} }
<script type="text/javascript">
function myOtherBox(e, id) {
if (e.value == 'otherprodtype')
{
$('#myBox_' + id).show();
}
else
{
$('#myBox_' + id).hide();
}
}
</script>
<tr style="display:none;">
<xsl:attribute name="id">myBox_<xsl:value-of select="@id"/></xsl:attribute>
<td class="Label">Other</td>
<td>
<input class="amdInputText" type="text" id="otherprodtypebox" value="">
<xsl:attribute name="value"><xsl:value-of select="otherprodtypebox"></xsl:value-of></xsl:attribute>
</input>
</td>
</tr>
else if($('#myBox.'+id+':visible'){if(!blank('otherprodtypebox[]){alert(“请输入有效的其他选项”);返回false;}
功能myOtherBox(e,id){
如果(e.value=='otherprodtype')
{
$('#myBox_'+id).show();
}
其他的
{
$('#myBox_'+id).hide();
}
}
我的盒子_
其他
编辑:
我现在几乎可以用了:
else if($('#myBox_<xsl:value-of select="@id"/>').is(':visible')) { if(!blank('otherprodtypebox[<xsl:value-of select="@id"/>]')){alert("Please enter a valid other option");return false;} }
else if($('#myBox')是(':visible'){if(!blank('otherprodtypebox[]){alert(“请输入有效的其他选项”);返回false;}
好的,所以这似乎只在框可见时检查验证,如果框为空则抛出警报,但由于某种奇怪的原因,当我填充框时,它不会像预期的那样抛出警报,但不会让我继续,并且没有错误消息我不太确定,但我认为这可能会导致问题(这是否返回任何结果?您可以检查): 相反,您可以尝试检查可见属性,如:
$('#myBox_' + id).attr('visible')
您应该知道,
$('#myBox'+id+':visible')
是一个jQuery选择器,因此如果没有匹配的元素,它将返回一个空数组
在JavaScript中将空数组转换为布尔值将始终返回true
。因此if($(“#myBox”+id+”:visible'))
将始终返回true
您可以使用
if($('#myBox.'+id+':visible').length>0)
来确保您的选择器至少匹配了一个元素。我成功地使它与多个元素组合使用,但最重要的是.is(':visible'))
工作代码:
else if(($('#myBox_<xsl:value-of select="@id"/>').is(':visible'))&&(!blank('otherprodtypebox[<xsl:value-of select="@id"/>]'))){alert("Please enter a valid other option");document.lending.otherprodtypebox[<xsl:value-of select="@id"/>].focus();return false; }
else if($('.'myBox')是(':visible'))和(!blank('otherprodtypebox[]){alert(“请输入有效的其他选项”);document.lending.otherprodtypebox[].focus();return false;}
将$('myBox.+id+':visible')
替换为$('myBox.+id.)('myBox.'visible')
严重修复了问题。$('myBox.+id.)这是应该的done@muluturk工作正常。谢谢帮助。我不能接受这个答案,因为它不是一个很好的参考资料。Pawel_W是对的。@Pawel_W工作正常。谢谢帮助。你的答案有帮助,但只是一个评论,所以不能给你答案points@topcat3好的,我已经为未来用户添加了答案,他们正在寻找解决方案我可以看到我的答案,以及添加到if中的答案。谢谢:)
else if(($('#myBox_<xsl:value-of select="@id"/>').is(':visible'))&&(!blank('otherprodtypebox[<xsl:value-of select="@id"/>]'))){alert("Please enter a valid other option");document.lending.otherprodtypebox[<xsl:value-of select="@id"/>].focus();return false; }