Number of jquery count inputs matching the required value

I have a form (with django forming) with the following contents:

<form action="./" id="my_form" method="post">
...
<select name="object_0_status" id="id_object_0_status">
<option value="">---------</option>
<option value="1">Online</option>
<option value="2">Offline</option>
<option value="3">Unknown</option>
</select>
<select name="object_1_status" id="id_object_1_status">
<option value="">---------</option>
<option value="1">Online</option>
<option value="2">Offline</option>
<option value="3">Unknown</option>
</select>
<select name="object_2_status" id="id_object_2_status">
<option value="">---------</option>
<option value="1">Online</option>
<option value="2">Offline</option>
<option value="3">Unknown</option>
</select>
    ...
</form>

I am writing a special validation method (using the JQuery Validations plugin http://docs.jquery.com/Plugins/validation ) that cross-validates different entries in the form. To do this, I need to get the number of selection fields for which their selected value is set to "1" (for example, online).

Selection codes are generated by the factory form, so their number will be variable. There are other selection options on the form that should not be considered - only those that end with "_status".

What is the cleanest way to do this?

thank

+3
source share
2
$('select[id$=_status] option:selected[value="1"]').length;

.

+7

ends with selector:

alert($('select[id$=_status]').find('option[value=1]').length);

, _status , find option , 1.

+2

Source: https://habr.com/ru/post/1788977/


All Articles