jQuery表单验证问题,其他框可见时
jQuery form validation issue, other box when visible
我有下面的"其他"框。我想验证这个框,这样它就不会只有在可见时才是空的。本应直截了当的是,我遇到了麻烦。非常感谢您的帮助。感谢
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'))&&(!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)
来确保您的选择器至少匹配了一个元素。
相关文章:
- 如何在.js函数中检索来自其他模板的表单的目标值
- 使用一个Javascript函数提交多个表单可以在FF中工作,而不需要其他浏览器
- 使用其他页面上表单的文本创建链接
- 在php中的同一网页上检索所选下拉项的值,并使用其他数据将其发布到其他表单
- 如何完成表单后使用 AJAX 重定向并在其他页面上显示数据
- HTML 按钮以在页面上的其他位置提交表单
- 停止jQuery表单验证重置由其他JavaScript填充的typeahead.js输入字段
- 如果用户单击其他按钮,则禁用表单元素
- 使用提交按钮提交表单,而不是页面上的其他按钮
- 当选中一个复选框时,表单中的所有其他复选框都必须禁用-不工作
- 使用AJAX值的Fileupload不会在表单中传递其他字段
- 表单验证io表单验证.如果“;“其他”;,请解释
- 如何在不干扰其他表单的情况下使用此javascript代码
- 如何在启用javascript时将表单替换为其他表单
- 将其他表单元素传递给ajax建议框脚本
- 当代码引用其他表单时,Javascript或表单验证
- 表单会影响其他表单和提交按钮禁用/启用
- 如果我单击订单表单,它应该显示,而其他表单应该隐藏
- 点击后调用其他表单
- 在其他表单中提交一个特定的动态生成表单