【问题标题】:jQuery. Remove "required" on hidden element when submitjQuery。提交时删除隐藏元素上的“必需”
【发布时间】:2014-03-15 09:39:45
【问题描述】:

我设法做复选框列表,使下拉选项在单击复选框时显示和隐藏,当我不选中任何复选框时,基本下拉选项将仅显示,并且需要选择这些下拉选项在点击提交之前。

问题是当任何一个复选框被选中时,基本的下拉选项会被隐藏,而其他下拉选项会显示,但是当点击提交按钮时,浏览器外的消息 pupup 显示仍然需要字段,这些字段是隐藏的下拉选项。

我想让这些下拉选项在隐藏时不需要。

这里是复选框的代码:

     var $j = jQuery.noConflict();


    $j(document).ready(function(){
        $j(':checkbox').bind('change', function() {
            var thisClass = $j(this).attr('class');
            if ($j(this).attr('checked')) {
                $j(':checkbox.' + thisClass + ":not(#" + this.id + ")").removeAttr('checked');
            }
            else {
                $j(this).attr('checked', false);
                $j('.main-options').show();
                $j('.first-image').show();
                $j('.hide-onload').hide();
            }
        });
    });

这是显示/隐藏的代码:

    $j(document).ready(function(e) {
        $j('.roundedOne').each(function(index, element) {
            $j(this).click(function(){
                $j('.main-options').hide();
                $j('.first-image').hide();
                $j('.hide-onload').hide();

                $j('.changing-options').eq(index).show();
                $j('.changing-image').eq(index).show();
            });
        });
    });

我希望我是清楚的。

【问题讨论】:

  • 你在使用 jqueryvalidation 吗?
  • 如果你使用 jqueryvalidation 你可以使用忽略属性,请检查forum.jquery.com/topic/…
  • 实际上,我正在使用 Magento 验证,但我在主联系表单中添加了新字段,其中包含 jquery 内容,因此现在混合在一起,我仅在需要某些内容时才使用“required”属性。这是页面的链接:cabas-durables.fr/magento/index.php/contacts 仍在处理中。

标签: jquery drop-down-menu required


【解决方案1】:

使用 jquery 试试这个解决方案。

...find('input, textarea, select') 您可以在表单中添加更多需要通过的元素

$('form#csf3SubmitForm').find('input, textarea, select').each(function(){
     if($(this).prop('required') && $(this).is(":hidden") && $(this).val() === ""){
         console.log("before -- IS REQUIRED >>> " +  $(this).prop('required') + " -- ID >>> " + $(this).attr("id") + " " + "VALUE >>> "  + $(this).val());
         $(this).prop("required", false);
         console.log("after -- IS REQUIRED >>> " +  $(this).prop('required') + " -- ID >>> " + $(this).attr("id") + " " + "VALUE >>> "  + $(this).val());
     }
 });

如果您的表单上有许多字段并且忘记添加进一步验证以不需要不可见字段,您可以遍历您的所有字段并检查哪些是必需的和不可见的......

将这些元素设置为 required=false。您可以在条件中添加更多内容,以具体说明您要禁用的一个或多个字段。

希望这会有所帮助并给出一个想法。这是一种解决方法。

【讨论】:

    猜你喜欢
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2017-01-18
    相关资源
    最近更新 更多