jQuery表单验证问题,其他框可见时

jQuery form validation issue, other box when visible

本文关键字:其他 表单 验证 问题 jQuery      更新时间:2023-09-26

我有下面的"其他"框。我想验证这个框,这样它就不会只有在可见时才是空的。本应直截了当的是,我遇到了麻烦。非常感谢您的帮助。感谢

    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_<xsl:value-of select="@id"/>').is(':visible')) { if(!blank('otherprodtypebox[<xsl:value-of select="@id"/>]')){alert("Please enter a valid other option");return false;} }

好的,所以这似乎只在框可见时检查验证,如果框为空则抛出警报,但由于某种奇怪的原因,当我填充框时,它没有像预期的那样抛出警报,而是不让我继续,并且没有错误消息

$('#myBox_' + id).is(':visible')替换$('#myBox_' + id + ':visible')可以修复问题

我设法让它与多种东西结合使用,但最重要的是.is(':visible')

工作代码:

else if(($('#myBox_<xsl:value-of select="@id"/>').is(':visible'))&amp;&amp;(!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; }

我不太确定,但我认为这可能会导致问题(这会返回任何结果吗?你可以检查一下):

$('#myBox_' + id + ':visible')

相反,您可以尝试检查可见属性,如:

$('#myBox_' + id).attr('visible')

您应该知道$('#myBox_' + id + ':visible')是一个jQuery选择器,因此如果没有匹配的元素,它将返回一个空数组。

在JavaScript中将空数组转换为布尔值将始终返回true。因此if($('#myBox_' + id + ':visible'))将始终返回true

您可以使用if($('#myBox_' + id + ':visible').length > 0)来确保您的选择器至少匹配了一个元素。