How to get all different outliers or not

I want to check if all the dropdown pages have a select value or not?

fox Ex: -

<select id="@model.Id">
     <option value="0">---</option>
     <option value="1">abc</option>
     <option value="2">xyz</option>
</select>

<select id="@model.Id">
     <option value="0">---</option>
     <option value="14">abc</option>
     <option value="25">xyz</option>
</select>

      

Both are not the same page and the problem is that dynamic id and name are assigned to both dropdrop down, so I can't use jQuery by id or name and get the selected value, but I don't want to make sure both selected a value by Javascript or jQuery?

How can i do this?

Best regards, Vinit

+3


source to share


3 answers


Try this, you can iterate over everything select

on the page with .each()

and compare with 0

. If the selection value 0

means it was not selected otherwise.

$(function(){
  $('select').each(function(){
     var value = $(this).val();
     var id = $(this).attr('id');
     if(value==0)
       alert('this dropdown has no selected value, id = '+id);
     else
       alert('this dropdown has selected value, id = '+id);
  }):
});

      



Edit - since the OP wants to check if both dropdowns are selected and then show the button, otherwise hide it, use below code

$(function(){
      $('select').change(function(){
        var totalUnselectedDropdown = $('select option[value="0"]:selected').length;
        if(totalUnselectedDropdown==0)
        {
          // enable button
          $('#buttonId').prop('disabled',false);
        }
        else
        {
          // disable button
          $('#buttonId').prop('disabled',true);
        }

      }):
    });

      

+2


source


you can do it like this:



$("select").each(function(){

alert($(this).val()) // will alert selected option value attribute value

if($(this).val() > 0) // value greater than 0 means value selected
{
 alert("Value Selected")
}

})

      

0


source


Try the following:

$("select").each(function(){
            if(parseInt($(this).val()) > 0){
                //all select have selected value
            }
            else
            {
                //any one/all select have not selected value
                return false;
            }
        });

      

0


source







All Articles