Marius Vasile

Marius Vasile

  • 592
  • 1.9k
  • 145.9k

asp.net core jquery to re-enable ddl ignore ddl source

May 7 2021 5:25 AM
I want to give user option to either select a value from ddl or to enter one in the textbox. However, since both are sources for binded table field, both shouldn't be active simultaneous so I use a jquery to disable the texbox if a value is selected in ddl or to re-enable if none. But when I try to do the same for the ddl, re-enable it lose the source for the ddl. How it should be done?
 
  1. <div class="row no-gutters" onchange="funcLocation()">  
  2.     <div class="col-md-2">  
  3.          <label class="form-control text-white" style="background-color:mediumorchid;">Location </label>  
  4.     </div>  
  5.     @if (Model.RALocation == true)  
  6.     {  
  7.         <div class="col-md-3">  
  8.            <select id="LocS" class="form-control border-danger" asp-items="@Model.SelectLocation" asp-for="RiskAssessmentMain.Location">  
  9.            <option value="">--Select Location--</option>  
  10.            </select>  
  11.                 </div>  
  12.                 <span class="text-danger ml-2 mr-2">OR</span>  
  13.             }  
  14.         <div class="col-md-3">  
  15.             <input id="LocT" class="form-control" asp-for="RiskAssessmentMain.Location" placeholder="Enter New Location" />  
  16.         </div>  
  17.     </div>  
jquery
 
  1. function funcLocation() {  
  2.             var data1 = $("#LocS :selected").text();  
  3.             var data2 = $("#LocT").val();  
  4.             if (data2.length > 0) {  
  5.                 $("#LocS").empty();  
  6.                 $("#LocS").attr("disabled"true);  
  7.             }  
  8.             else {  
  9.                 $("#LocS").attr("disabled"false);  
  10.                 $("#LocS").append("<option value=''>--Select Location--</option>");  
  11.             }  
  12.             if (data1 != "--Select Location--") {  
  13.                 $("#LocT").empty();  
  14.                 $("#LocT").attr("disabled"true);  
  15.             }  
  16.             else {  
  17.                 $("#LocT").attr("disabled"false);  
  18.             }  
  19.         }  
 

Answers (4)