There are two drop down lists. It is necessary to make it so that if Chelyabinsk is selected in the first list, then Grape is selected in the second, if any other value is selected, the element with the value - 0 ("Second list") is automatically selected. Now, despite the condition, it is always always selected "Grape".

$('#edit-field-sity-tid').change(function() { var SVal = $(this).find('option:selected').val(); var c = $('#edit-field-streets').find('option'); if (SVal == 710) { $('.dependent').css('display', 'block'); c.val(2).attr('selected', 'selected'); } else { c.val(0).attr('selected', 'selected'); $('.dependent').css('display', 'none'); } }); 
 .dependent { display: block; position: relative; background: #8F8F8F; margin: 20px; color: #fff; padding: 7px; text-align: center; } .hide { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <select id="edit-field-sity-tid" name="field_sity_tid" class="form-select"> <option value="All">Элементы списка</option> <option value="708">Златоуст</option> <option value="710">Челябинск</option> </select> <select id="edit-field-streets" name="field_sity_tid" class="form-select"> <option value="0">Второй список</option> <option value="1">Каштановая</option> <option value="2">Виноградная</option> </select> <div class="dependent hide">А я тут для проверки условия))</div> 

    1 answer 1

    Most likely so, or in any other way. The difference between prop and attr is illustrated here .

     $(function() { $('#edit-field-sity-tid').on('change', function() { var SVal = $(this).val(); var c = $('#edit-field-streets').find('option'); if (SVal == 710) { c.filter("[value=2]").prop('selected', 'selected'); } else { c.filter("[value=0]").prop('selected', 'selected'); } }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select id="edit-field-sity-tid" name="field_sity_tid" class="form-select"> <option value="All">Элементы списка</option> <option value="708">Златоуст</option> <option value="710">Челябинск</option> </select> <select id="edit-field-streets" name="field_sity_tid" class="form-select"> <option value="0">Второй список</option> <option value="1">Каштановая</option> <option value="2">Виноградная</option> </select> 

    • I forgot about prop completely :( - splash58
    • But already faced with prop. my garden head ... I don’t understand only one line)) $ (this) .val (); We're talking about the value of the select and we have it, the select has no values, but why then does everything work?) - Evgeny Shevtsov
    • he has a value, so you can find out who is selected, but you cannot set it to change the choice - splash58
    • and here it is var c = $('#edit-field-streets').find('option'); then it can be replaced by var c = $('this).find('option'); so as not to look again. - splash58
    • one
      @ splash58 no. this refers to the first list, and the variable c gets the value from the second. - Evgeny Shevtsov