jquery - How to require a field with a value of "yes" using javascript? -



jquery - How to require a field with a value of "yes" using javascript? -

i need able require fields if selects value of "yes" dropdown. i've used next code doesn't seem work.

$(function () { $('#anyadditionalinc').keyup(function () { if ($(this).val() == "no") { $('#additionalincomesource').removeattr('required'); $('#additionalincomeamt').removeattr('required'); } else { $('#additionalincomesource').attr('required', 'required'); $('#additionalincomeamt').attr('required', 'required'); } }); });

my dropdown looks this

<div class="form-group">@html.labelfor(m => m.anyadditionalinc, new { @class = "col-sm-2 control-label" }) <div class="col-sm-10"> <div class="col-sm-4">@html.dropdownlistfor(m => m.anyadditionalinc, new selectlist(new list <object>{ new { value = "", text = "----"}, new { value = "yes", text = "yes"}, new { value = "no", text = "no"}, }, "value", "text"), new { @class = "form-control", id = "anyadditionalinc" }) @html.validationmessagefor(m => m.anyadditionalinc)</div> </div> </div>

any help appreciated. doesnt seem want require validation on source , amt fields when selecting yes.

a dropdown (i guess mean <select> element that) doesn't have much keyup events. seek change instead:

$(function () { $('#anyadditionalinc').change(function () { var active = $(this).val() != "no"), fields = $('#additionalincomesource, #additionalincomeamt'); fields.prop('required', active); if (!active) fields.val(""); }); });

javascript jquery asp.net-mvc-4

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -