【问题标题】:JQUERY - how do I validate/check my check-boxes/radios in my code structure?JQUERY - 如何验证/检查代码结构中的复选框/收音机?
【发布时间】:2012-06-16 19:33:56
【问题描述】:

我让这件事变得更糟、更困难,但我想不通。

我需要为我们正在工作的特殊客户端应用程序进行一些自定义验证编码,所以不能使用 jquery val 插件

我想要的只是一个复选框和单选验证功能,它与我为文本字段验证构建的基本相同。我的复选框和无线电验证代码太糟糕了,它破坏了最初工作的文本字段验证

我已经消除了我遇到的非工作代码灾难 - 可以请一些人告诉我如何工作吗?

使用我的 jsfiddle/下面的代码结构:当您单击“btnCatchReqFlds”按钮时,我希望它运行文本字段检查,然后检查复选框,单选检查并显示所有必需但未填写的字段-in/checked/selected.

查看 jsfiddle,您将了解它是如何与文本字段验证一起工作的。我只需要使用复选框/单选按钮合并相同的功能/检查。

我想我很接近,非常接近。我已经更新了代码,我知道这不是很好的编码,但要逐步获得我需要的东西。下面的代码现在检查必需但为空的文本和检查/单选字段。现在的问题是,代码抓取了正确的字段,但是“.not(':checked');”无法正常工作。如果我选中其中一个收音机/复选框,我会得到相同的返回值。我做错了什么:return $(this).not(':checked');

my jsfiddle:

jquery:

$("#btnCatchReqFlds").on('click', function()  
{
    $("#holdErrMsg").empty();
    $("#holdErrMsg_checkRadios").empty();
    var requiredButEmpty = $("fieldset:visible").find('input[class*="-required"], select[class*="-required"]').filter(function() 
            {
                return $.trim($(this).val()) === "";  
            });
    var chk_requiredButEmpty = $("fieldset:visible").find(":input:checkbox[class*='-required'],:input:radio[class*='-required']").filter(function()
            {
                return $(this).not(':checked');
            });
    if (requiredButEmpty.length) 
        {
            requiredButEmpty.each(function () 
                {
                    $("#holdErrMsg").append("Please fill in the " + this.name + "<br />");
                });
        }
    if (chk_requiredButEmpty.length) 
    {
        chk_requiredButEmpty.each(function () 
            {
                $("#holdErrMsg_checkRadios").append("Please fill in the " + this.name + "<br />");
            });
    }
    return !requiredButEmpty.length;
    return !chk_requiredButEmpty.length;
});

HTML

<form method="post" action="">
    <div id="holdErrMsg"></div>
    <div id="holdErrMsg_checkRadios"></div>
    <fieldset id="mainSection" name="mainSection">
        <legend style="color:blue; font-weight:bold">Project Overview Section</legend>
        <table style="width: 100%">
            <tr>
                <td style="height: 33px; width: 178px;">Name<span style="color: red">*</span></td>
                <td style="height: 33px"><input id="1125" name="1125" class="1125-required" type="text" /> - 1125</td>
            </tr>
            <tr>
                <td style="height: 33px; width: 178px;">Email<span style="color: red">*</span></td>
                <td style="height: 33px"><input id="1026" name="1026" class="1026-required" type="text" /> - 1126</td>
            </tr>
            <tr>
                <td style="width: 178px">Product Title</td>
                <td><input id="1089" name="1089" type="text" /></td>
            </tr>
            <tr>
                <td style="width: 178px">Product Type</td>
                <td>
                    <select id="1169" name="1169">
                        <option value="">Select</option>
                        <option value="Cars">Cars</option>
                        <option value="Boats">Boats</option>
                        <option value="Planes">Planes</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <button id="btnCatchReqFlds" type="button" name="btn">Check Required Fields</button>
                </td>
            </tr>
        </table>
    </fieldset>
    <!-- Car Section  -->
    <fieldset id="section-11" name="section-11">
        <legend style="color:fuchsia; font-weight:bold">Car Details Section</legend>
        <table cellpadding="2" style="width: 100%">
            <tr>
                <td style="width: 334px; height: 35px"><label>Size:<span style="color: red">*</span></label></td>
                <td style="height: 35px"><input id="1245" class="1245-required" name="1245" type="text" /> - 1245</td>
            </tr>
            <tr>
                <td style="height: 35px; width: 334px">Color:<span style="color: red">*</span></td>
                <td style="height: 35px">
                    <select id="1433" class="1433-required" name="1433">
                        <option value="">Select</option>
                        <option value="Orange">Orange</option>
                        <option value="Blank">Blank</option>
                        <option value="Green">Green</option>
                    </select>
                    - 1433
                </td>
            </tr>
            <tr>
                <td style="width: 334px">Description:</td>
                <td>
                    <textarea id="1290" name="1290" rows="2" style="width: 433px"></textarea>
                </td>
            </tr>
        </table>
    </fieldset>
    <!-- Plane Section  -->
    <fieldset id="section-12" name="section-12">
        <legend style="color:fuchsia; font-weight:bold">Plane Details Section</legend>
        <table cellpadding="2" style="width: 100%">
            <tr>
                <td style="width: 334px; height: 35px"><label>Size:</label></td>
                <td style="height: 35px"><input id="1245" name="1245" type="text" /></td>
            </tr>
            <tr>
                <td style="height: 35px; width: 334px">Color<span style="color: red">*</span>:</td>
                <td style="height: 35px">
                    <input type="checkbox" name="1433[]" id="1433[]" value"Orange" class="1433[]-required" />Orange
                    <input type="checkbox" name="1433[]" id="1433[]" value"Blue" class="1433[]-required" />Blue
                    <input type="checkbox" name="1433[]" id="1433[]" value"Green" class="1433[]-required" />Green
                    |  1302
                </td>
            </tr>
            <tr>
                <td style="width: 334px">Description:</td>
                <td>
                    <textarea id="1290" name="1290" rows="2" style="width: 433px"></textarea>
                </td>
            </tr>
        </table>
    </fieldset>
    <!-- Boat Section  -->
    <fieldset id="section-13" name="section-13">
        <legend style="color:fuchsia; font-weight:bold">Boat Details Section</legend>
        <table cellpadding="2" style="width: 100%">
            <tr>
                <td style="width: 334px; height: 35px"><label>Size:</label></td>
                <td style="height: 35px"><input id="1245" name="1245" type="text" /></td>
            </tr>
            <tr>
                <td style="height: 35px; width: 334px">Color:<span style="color: red">*</span></td>
                <td style="height: 35px">
                    <input type="radio" name="1834" id="1834" value="None" class="valuetext 1834-required" />None
                    <input type="radio" name="1834" id="1834" value="All" class="valuetext 1834-required" />All
                    - 1834
                </td>
            </tr>
            <tr>
                <td style="width: 334px">Description:</td>
                <td>
                    <textarea id="1290" name="1290" rows="2" style="width: 433px"></textarea>
                </td>
            </tr>
        </table>
    </fieldset>
    <br />
    <!-- Misc. Info Section  -->
    <fieldset id="section-1011" name="section-1011">
        <legend style="color:green; font-weight:bold">Misc. Info Section</legend>
        <table cellpadding="2" style="width: 100%">
            <tr>
                <td style="width: 334px; height: 35px"><label>Size:</label></td>
                <td style="height: 35px"><input id="1301" name="1301" type="text" /></td>
            </tr>
            <tr>
                <td style="height: 35px; width: 334px">Color:</td>
                <td style="height: 35px">
                    <select id="1302" name="1302">
                        <option value="Orange">Orange</option>
                        <option value="Blank">Blank</option>
                        <option value="Green">Green</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td style="width: 334px">Description:</td>
                <td>
                    <textarea id="1303" name="1303" rows="2" style="width: 433px"></textarea>
                </td>
            </tr>
        </table>
    </fieldset>
</form>

【问题讨论】:

  • 我在小提琴中看不到任何单选按钮或复选框
  • 对不起,我应该提到您需要使用产品类型下拉菜单。

标签: jquery validation button checkbox radio


【解决方案1】:

我做错了什么:return $(this).not(':checked');

您假设.not.is 的倒数,但事实并非如此。 .not 返回一个布尔值,它返回一个 jQuery 对象,其中包含与选择器不匹配的元素。

你应该简单地使用:

!$(this).is(":checked")

确定是否未检查某些内容。甚至更好:

!this.checked

更新小提琴: http://jsfiddle.net/VSx6M/


其他一些注意事项:

  • 在事件处理程序的末尾有两个return 语句。第二个是无法访问的,因为return 立即将程序流返回给调用函数。
  • 数组文字 ([]) 表示法优于 new Array
  • 单选按钮被多次验证,因为您没有考虑单选按钮组。这会导致单选按钮被标记为无效,因为它们都被标记为必需。

*我知道您不能使用 jQuery 验证,但我强烈建议您尝试一下。

【讨论】:

    【解决方案2】:

    在您最近的更新中,您需要做的就是检查是否检查了所有兄弟姐妹,然后从它返回给您的变量的元素数组中抓取一个。如果您在开始附加错误消息之前插入下面的代码,它应该按照您想要的方式工作。这是一个更新的小提琴http://jsfiddle.net/mPXAw/7/

    var chk_requiredButEmpty = $("fieldset:visible").find(":checkbox[class*='-required'],:radio[class*='-required']").filter(function()
    {
           if(!$(this).siblings().is(':checked')){
                   return !$(this).is(':checked');
           }
    });
    chk_requiredButEmpty = chk_requiredButEmpty.eq(0);
    

    【讨论】:

    • 您好,wirey,非常感谢您的尝试。我想知道原始代码是否有什么东西。问题是,这是一个示例应用程序,因此可能有 3 种产品类型或 50 种产品类型。可能有 20 个部分和 200 个必填字段,因此代码更加动态。
    • 它不能与原始代码一起工作的原因是因为您现在有文本框,您可以在其中比较值是否为空字符串。在这里你不能,因为要么检查要么不检查,除非你创建一个包含值的隐藏字段然后检查该隐藏字段。
    • Wirey - 我认为这确实有效。谢谢。你不知道我和这个斗争了多久。谢谢!!!!!!
    • @user1176783 哦,你回来看看我更新的答案:P 我很高兴能提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    相关资源
    最近更新 更多