同时选中两个复选框时,使输入部分为必填项 [英] Make input section required when both checkboxes are selected

查看:216
本文介绍了同时选中两个复选框时,使输入部分为必填项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当我分别单击两个复选框时,将使底部成为必填项-选中这两个复选框时,如何使我成为最后一个必填项?我似乎不知道该怎么做.

So I have two checkboxes when clicked separately, will make the bottom section required - How can I make it that the last section is required when both are checked? I can't seem to figure out how to do that.

概述:
选中两个复选标记时,将我确认..."部分设为红色(必填).

Overview:
Make the 'I confirm ...' section be red (required) when both checkmarks as checked.

#rtd3Transaction是我的交易数据".
.rtdConfirm是上一节的课程.

#rtd3Transaction is 'My transaction data'.
.rtdConfirm is the class from the last section.

代码:

/* Make checkbox textboxes not required unless checked */
$(document).ready(function() {
    $('#rtd3Transaction').change(function() {
        if ($('.rtdConfirm').attr('required')) {
            $('.rtdConfirm').removeAttr('required');
        }
        else {
            $('.rtdConfirm').attr('required','required');
        }
    });
});

$(document).ready(function() {
    $('#rtd3Device').change(function() {
        if ($('.rtdConfirm').attr('required')) {
            $('.rtdConfirm').removeAttr('required');
        }
        else {
            $('.rtdConfirm').attr('required','required');
        }
    });
});

推荐答案

我看到您正在使用jQuery,所以这是一个基于jQuery的解决方案.

I see you're using jQuery, so here's a jQuery-based solution.

首先,您可以为两个输入组合 change 事件处理程序.

First, you can combine the change event handler for both inputs.

第二,您可以使用 .prop("checked")查找复选框的选中状态.

Second, you can use .prop("checked") to find the checked state of your checkboxes.

最后,使用 .prop("required", true/false )设置最后一个复选框的必需状态.

Finally, use .prop("required", true/false) to set the required state of your last checkbox.

这是我的处理方式:

$(document).ready(function()
{
    $("#rtd3Transaction, #rtd3Device").change(function()
    {
        var required = $("#rtd3Transaction").prop("checked") && $("#rtd3Device").prop("checked");
        $(".rtdConfirm").prop("required", required);
    });
});

这篇关于同时选中两个复选框时,使输入部分为必填项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆