Populate dropdown select with array using jQuery

后端 未结 8 1908
花落未央
花落未央 2020-11-29 08:07

I am trying to populate a dropdown select with an array using jQuery.

Here is my code:

        // Add the list of numbers to the drop down here
              


        
8条回答
  •  情书的邮戳
    2020-11-29 08:43

    function validateForm(){
        var success = true;
        resetErrorMessages();
        var myArray = [];
        $(".linkedServiceDonationPurpose").each(function(){
            myArray.push($(this).val())
        });
    
        $(".linkedServiceDonationPurpose").each(function(){
        for ( var i = 0; i < myArray.length; i = i + 1 ) {
            for ( var j = i+1; j < myArray.length; j = j + 1 )
                if(myArray[i] == myArray[j] &&  $(this).val() == myArray[j]){
                    $(this).next( "div" ).html('Duplicate item selected');
                    success=false;
               }
            } 
        });
        if (success) {
            return true;
        } else {
            return false;
        }
        function resetErrorMessages() {
            $(".error").each(function(){
                $(this).html('');
            });``
        }
    }
    

提交回复
热议问题