Dropdownchecboxes selected value validation in javascript

后端 未结 1 1712
萌比男神i
萌比男神i 2020-12-22 11:11

I have a DropDownCheckBoxes where I can CHECK multiple items from the list. Now what I want is,

A validation on button click for multiple Items selected

1条回答
  •  忘掉有多难
    2020-12-22 11:45

    So I took a look at your code. And since I didn't know it you wanted it in plain Javascript or jQuery, I took the liberty of doing it in jQuery (since it's easier).

    Also I don't know the true purpose of your code, so I based it upon your example and the information you gave us. This might give you an idea on how to complete your task.

    Either run the snippet below or take a look at this fiddle.

    /* Slide open menu and change arrow direction */
    $("#caption").click(function() {
        $("#cmbEmp_Name_dv").slideToggle();
        $("#down, #up").toggle();
    });
    
    /* On change disable all checkboxes from different months */
    $("input[type='checkbox']").change(function() {
        var amountChecked = $("input[type='checkbox']:checked").length;
        if (amountChecked === 1) {
            var month = getMonth($(this).next().html());
            $("input[type='checkbox']").each(function() {
                var myMonth = getMonth($(this).next().html());
                if (month !== myMonth) {
                    $(this).prop("disabled", true);
                    $(this).next().css("background-color", "gray");
                }
            });
        }
    
        if (amountChecked === 0)
            $("input[type='checkbox']").prop("disabled", false).next().css("background-color", "transparent");
    });
    
    /* Function to check if all checked options are from the same month */
    $("#btnSubmit").click(function() {
        var firstSelected = $("input:checked").first().next().html();
        if (firstSelected !== undefined && firstSelected !== "undefined" && firstSelected && firstSelected != "") {
            var firstMonth = getMonth(firstSelected);
            var isNotEqual = false;
    
            $("input:checked").each(function() {
                var month = getMonth($(this).next().html());
    
                if (firstMonth !== month)
                    isNotEqual = true;
            });
    
            if (isNotEqual)
                alert("Please check items from the month " + firstMonth);
            else
                alert("Validation complete - good to go!");
        }
    	else
    	{
    		alert("Select an option first!");
    		$("#cmbEmp_Name_dv").slideDown();
    	}
    });
    
    
    
    /* Function to strip off all characters and return the month */
    function getMonth(html) {
        var monthPart = html.split("(").length > 1 ? html.split("(")[1] : "-";
        return monthPart.substr(0, monthPart.indexOf("-")).trim();
    }
    
    
    Select
      

    Notice
    I did implement @Freak his suggestion, by disabling all the options different from the user his first choice.

    On the other hand, I also implemented the check on the Validate button, where as it will check if the user may or may not have checked a different month. Just in case.

    0 讨论(0)
提交回复
热议问题