字段隐藏时的javascript验证

字段隐藏时的javascript验证,javascript,jquery,validation,Javascript,Jquery,Validation,我有下面的代码,其中有一个下拉列表。仅当选择了“其他”选项时,下拉列表才会显示。我想进行验证,仅当选择“其他”选项以验证字段不为空时才进行检查 此行用于验证,但当框隐藏时也会弹出,以便查找额外的检查。我有我的尝试在底部,但必须有一个更好的俏皮的方式,工作。谢谢 if(!blank('otherprodtypebox[<xsl:value-of select="@id"/>]')){alert("Please enter a valid other option");return fa

我有下面的代码,其中有一个下拉列表。仅当选择了“其他”选项时,下拉列表才会显示。我想进行验证,仅当选择“其他”选项以验证字段不为空时才进行检查

此行用于验证,但当框隐藏时也会弹出,以便查找额外的检查。我有我的尝试在底部,但必须有一个更好的俏皮的方式,工作。谢谢

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>
    <td class="Label">Product Type</td>
    <td class="field">
        <select name="producttypes">
            <xsl:attribute name="onchange">myOtherBox(this, '<xsl:value-of select="@id"/>'); checkBoxVisible(this, '<xsl:value-of select="@id"/>')</xsl:attribute>
            <xsl:attribute name="id">producttypes_<xsl:value-of select="@id"/></xsl:attribute>

            <option value="a">
                <xsl:if test="producttypes/option[@id='a']='selected'">
                    <xsl:attribute name="selected"/>
                </xsl:if>A</option>
            <option value="b">
                <xsl:if test="producttypes/option[@id='b']='selected'">
                    <xsl:attribute name="selected"/>
                </xsl:if>B</option>
            <option value="otherprodtype">
                <xsl:if test="producttypes/option[@id='otherprodtype']='selected'">
                    <xsl:attribute name="selected"/>
                </xsl:if>Other</option>
        </select>
    </td>
</tr>
<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>



if($('#myBox_' + id).validate({ ignore: ':hidden' });) { if(!blank('otherprodtypebox[<xsl:value-of select="@id"/>]')){alert("Please enter a valid other option");return false;} }
if(!blank('otherprodtypebox[]){alert(“请输入有效的其他选项”);返回false;}
功能myOtherBox(e,id){
如果(e.value=='otherprodtype')
{
$('#myBox_'+id).show();
}
其他的
{
$('#myBox_'+id).hide();
}
}
产品类型
myOtherBox(这个“);复选框可见(此为“”)
产品类型_
A.
B
其他
我的盒子_
其他
if($('#myBox_'+id).validate({ignore:':hidden'}){if(!blank('otherprodtypebox[])){alert(“请输入有效的其他选项”);返回false;}

使用类进行验证。也就是说,验证一个名为validator的类

在您的JS中:

function myOtherBox(e, id) {
    if (e.value == 'otherprodtype')
    {
        $('#myBox_' + id).show().addClass('validator');
    }
    else
    {
        $('#myBox_' + id).hide().removeClass('validator');
    }
}
编辑:或者,您可以将其用作选择器,而不是上述选项:

  if($('#myBox_' + id + ':visible').....................etc

那会更好。

不知道验证器。所以我假设我添加了这个类,然后为了验证,我可以使用if($('#myBox_'+id).validate();)或类似的东西?感谢它的工作,请看是的,在我的编辑我建议使用:可见在您的选择器。我引用的“验证器”是一个CSS类,但实际上:visible是一个更好的方法,这就是我编辑的原因。谢谢