Marius Vasile

Marius Vasile

  • 594
  • 1.9k
  • 144.8k

asp.net core jquery not functioning when ddl is empty

May 16 2021 1:24 PM
I have a select and a textbox handled by a jquery. Basically, if I select a value from ddl, the textbox became disabled and if I enter text in the textbox, the ddl became disabled. The problem I have is: If the ddl is empty, the jquery does not work. How can I fix this?
  
  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">  
  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" placeholder="Enter New Location" />  
  16.                 <input id="loc" asp-for="RiskAssessmentMain.Location"  />  
  17.             </div>  
  18.         </div>  
 
  1. function funcLocation() {  
  2.             var data1 = $("#LocS :selected").text();  
  3.             var data2 = $("#LocT").val();  
  4.             if (data2.length > 0) {  
  5.                 $("#LocS").attr("disabled"true);  
  6.                 $("#loc").val(data2);  
  7.             }  
  8.             else {  
  9.                 $("#LocS").attr("disabled"false);  
  10.             }  
  11.             if (data1 != "--Select Location--") {  
  12.                 $("#LocT").empty();  
  13.                 $("#LocT").attr("disabled"true);  
  14.                 $("#loc").val(data1);  
  15.             }  
  16.             else {  
  17.                 $("#LocT").attr("disabled"false);  
  18.             }  
  19.         }  
 
 
 

Answers (3)