Tell me. How to make it so that it requires filling inbound_from only when it is visible (active).

Requires me even if inbound_from is hidden

if ($('#inbound_from').val() == null) { $('.errorrep').append('<h3>Выберете поле От кого</h3>'); $('.errorrep').css({'display':'block'}); $('#draggable').animate({ scrollTop: 0 }); return false; } 

HTML

 <select id="inbound_from" name="blockger2016.inbound_from" onchange="mzchange()" style="display: block;"> <option value="" disabled="" selected="">От кого</option> <option value="1">ГРАЖДАНИНОМ (ЛИЧНО)</option> <option value="2">Ф</option> <option value="3">Т </option> </select> 

    1 answer 1

     if ($('#inbound_from').hasClass("active") && $('#inbound_from').val() == null) { // ... } 

    or

     if ($('#inbound_from').css("display") != "none" && $('#inbound_from').val() == null) { // ... } 

    And please, - "Choose ..."


     function hide() { $('#inbound_from').hide(); } function show() { $('#inbound_from').show(); } function check() { if ($('#inbound_from').css("display") != "none" && $('#inbound_from').val() == null) { console.log("Error: option is not selected."); return; } console.log("All is good."); } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="inbound_from" name="blockger2016.inbound_from" style="display: block;"> <option value="" disabled="" selected="">От кого</option> <option value="1">ГРАЖДАНИНОМ (ЛИЧНО)</option> <option value="2">Ф</option> <option value="3">Т </option> </select> <button onclick="hide()">Hide</button> <button onclick="show()">Show</button> <button onclick="check()">Check</button> 

    • Does not require a choice, I added html in the task - dmr
    • @dmr What "Doesn't require a choice"? - Igor
    • When "From Whom" is needed so that a choice of value is required, it does not require it - dmr
    • @dmr Yes, indeed, $(select).val() with the option selected with the disabled attribute returns null , not "" . - Igor
    • Thanks so much - dmr