循环查看jquery页面上的可见文本框

循环查看jquery页面上的可见文本框,jquery,html,validation,Jquery,Html,Validation,我试图以可见的形式检查页面上的文本框,并验证它们是否为空。我有两个隐藏的文本框,当用户选择某些项目时,这些文本框会自动取消隐藏。我试图跳过这些隐藏的文本框,除非它们是可见的。我尝试了这段代码,但它仍然警告不可见的文本框没有值 jQuery: <script type="text/javascript"> $(function () { $('#selected1').val(""); $('#selected2').val(""); $('select').

我试图以可见的形式检查页面上的文本框,并验证它们是否为空。我有两个隐藏的文本框,当用户选择某些项目时,这些文本框会自动取消隐藏。我试图跳过这些隐藏的文本框,除非它们是可见的。我尝试了这段代码,但它仍然警告不可见的文本框没有值

jQuery:

<script type="text/javascript">
$(function () {
    $('#selected1').val("");
    $('#selected2').val("");

    $('select').change(function () {
        if ($('#DDLHearAboutUs').val() === "Optional1") {
            $('#OptionalTxt1').show();
        } else {
            $('#OptionalTxt1').hide();
        }
        if ($('#DDLProfession').val() === "Optional2") {
            $('#OptionalTxt2').show();
        } else {
            $('#OptionalTxt2').hide();
        }
    });

});


function checkEmpty() {
    var empty = false;
    $('select').each(function () {
        if ($(this)[0].selectedIndex === 0) {
            empty = true;
        }

    });
    $('form input:text:visible').each(function () {
        if ($(this).val().length === 0) {
            empty = true; 
        }
    });


    if (empty) {
        alert('Please fill out all of the fields');
        return false;
    }
}
</script>

$(函数(){
$('#selected1').val(“”);
$('#selected2').val(“”);
$('select')。更改(函数(){
if($('#DDLHearAboutUs').val()=“可选1”){
$('#OptionalTxt1').show();
}否则{
$('#OptionalTxt1').hide();
}
如果($('#DDLProfession').val()=“可选2”){
$('#OptionalTxt2').show();
}否则{
$('#OptionalTxt2').hide();
}
});
});
函数checkEmpty(){
var empty=false;
$('select')。每个(函数(){
if($(此)[0]。selectedIndex==0){
空=真;
}
});
$('form input:text:visible')。每个(函数(){
if($(this).val().length==0){
空=真;
}
});
if(空){
警报(“请填写所有字段”);
返回false;
}
}
HTML:

你是怎么听说我们的 --挑选-- 网站 邮寄 电子邮件 送交 可选:

请注明您的职业: --挑选-- 职业治疗师 职业治疗助理 心理治疗师 物理治疗助理 训练师 其他:

全名:

邮寄地址:

电子邮件:

电话号码:

算出了。。我使用了:
$('input[type=“text”]:visible')
作为选择器,而不是
$('form input:text:visible')
可能多个冒号的语法不正确……但效果很好。

您是否在不可见的文本框上放置一个类来隐藏它们?尝试搜索所有文本框,并包含一个if语句,该语句表示“没有类makeMeInvisible”。不,要隐藏它们,我有另一个函数,它会对DropDownList激发onchange,如果该值=我要查找的值,那么它只会对调用
checkEmpty
的文本框执行.show()或.hide()?哦,对不起。这只是ASP.NET按钮。我必须设置为
OnClientClick=“return checkEmpty();”
<label><strong>How did you hear about us? &nbsp;</strong>
</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<select name="DDLHowDidYouHearAboutUs" id="DDLHearAboutUs" required="required" style="height: 20px;">
    <option style="display: none;" id="selected1" disabled="true" selected="selected">--Select--</option>
    <option value="Website">Website</option>
    <option value="Mailing">Mailing</option>
    <option value="Email">Email</option>
    <option value="Referral">Referral</option>
    <option value="Optional1">Optional:</option>
</select>
<input type="text" style="width: 160px; height: 20px; display: none;" id="OptionalTxt1" />
<br />
<br />
<label><strong>Please indicate your profession: &nbsp;</strong>
</label>
<select style="height: 20px;" name="DDLProfession" id="DDLProfession" required="required">
    <option selected="selected" disabled="disabled" style="display: none;" id="selected2">--Select--</option>
    <option value="OccupationalTherapist">Occupational Therapist</option>
    <option value="OccupationalTherapyAssistant">Occupational Therapy Assistant</option>
    <option value="PhyiscalTherapist">Phyiscal Therapist</option>
    <option value="PhyiscalTherapyAssistant">Phyiscal Therapy Assistant</option>
    <option value="AthleticTrainer">Athletic Trainer</option>
    <option value="Optional2">Other:</option>
</select>
<input type="text" style="width: 160px; height: 20px; resize: none; display: none;" id="OptionalTxt2" />
</td>
</tr>
<tr>
    <td align="left" valign="top">
        <br />
        <br />
        <label><strong>Full Name: &nbsp; &nbsp; &nbsp; &nbsp; </strong></label>&nbsp;&nbsp;
        <input type="text" style="width: 235px;" name="FullName" required="required" /><br />
        <br />
        <label><strong>Mailing Address: &nbsp;</strong></label>
        <input type="text" style="width: 235px;" name="Address" required="required" /><br />
        <br />
        <label><strong>Email: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</strong></label>&nbsp;
        <input type="textl" name="Email" style="width: 235px;" required="required" /><br />
        <br />
        <label><strong>Phone Number: </strong></label>&nbsp;&nbsp;&nbsp;
        <input type="text" name="Phone" required="required" />
        <br />