0

我有下面的代码,它有一个下拉菜单。the dropdown appears only when the other option is selected. 我想做验证,仅在选择另一个选项来验证字段不为空时检查

此行适用于验证,但在隐藏框时也会弹出,因此需要额外检查。我有我的尝试在底部,但必须有一个更好的漂亮的方法。谢谢

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;} }
4

1 回答 1

1

使用类进行验证。也就是说,验证一个名为 .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

那会更好。

于 2013-02-07T17:51:22.580 回答