2016-08-16 102 views
0

我想創建一個函數,當輸入郵政編碼時,如果城市中有條目,它會清除表單中的該字段並顯示城市列表。在表單上清除結果

當用戶根據包含城市列表的下拉框輸入城市時也是如此。用戶選擇城市後,郵政編碼字段留空。

在藉助stackoverflow的幫助下,

我能夠獲得這個。但是,由於某些原因,它只適用於郵政編碼字段。這意味着,一旦清除了城市中的字段,我就可以根據郵政編碼檢索列表項並繼續更改郵政編碼。當我選擇一個城市時,郵政編碼字段將被清除,但不會顯示新的結果。

不知道發生了什麼事。

$('#zip').on('input', function() { 
 
    $('#city').val("") 
 
}) 
 
$('#city').on('input', function() { 
 
    $('#zip').val("")
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<div class="panel panel-default"> 
 
\t <div class="panel-body"> 
 
<!--- \t <div id="loader" style="position: fixed; top:0; left:0; width:100%; height: 100%; background: url('loader.gif') center center #efefef"></div><!--Progress bar---> 
 
\t \t <form name="providerSearch" ng-submit="SearchProvider(searchParam);" novalidate="" role="form"> 
 
\t \t \t <div class="form-group"><input class="form-control" id="physiciansfirstname" ng-model="searchParam.FirstName" placeholder="First name:" type="text" /></div> 
 

 
\t \t \t <div class="form-group"><input class="form-control" id="physicianslastname" ng-model="searchParam.LastName" placeholder="Last name:" type="text" /></div> 
 

 
\t \t \t <div class="form-group"><select class="form-control" id="providerSpecialty" ng-model="searchParam.Specialty"><option disabled="disabled" selected="selected" value="">Specialty</option> 
 
      <option value=""></option><option>Family practice</option><option>General practice</option><option>Internal medicine</option><option>Pediatrics</option> </select></div> 
 

 
\t \t \t <div class="form-group"> 
 
      <SELECT name="proCity" class="form-control" id="city" placeholder="City" ng-model="searchParam.City"> 
 
\t \t \t \t \t <option disabled="disabled" selected="selected" value="">City</option> 
 
\t \t \t \t \t \t <option value=""></option> 
 
\t \t \t \t \t \t <cfoutput query="cityFind"> 
 
          <option value=#city#>#city#</option> 
 
\t \t \t \t \t \t </cfoutput> 
 
\t \t \t \t \t </select> 
 
         
 
      <!---<select class="form-control" id="city" ng-model="searchParam.City"><option disabled="disabled" selected="selected" value="">City</option><option ng-repeat="c in Cities">{{c.City}}</option> </select>----> 
 
      </div> 
 

 
\t \t \t <div class="row"> 
 
\t \t \t \t <div class="col-xs-6 no-right-padding paddingLanguage"> 
 
\t \t \t \t \t <div class="form-group widthLanguage"> 
 
       
 
         
 
        <select name="language" class="form-control" ng-model="searchParam.Language"> 
 
\t \t \t \t \t  <option disabled="disabled" selected="selected" value="">Language</option> 
 
         <option value=""></option> 
 
\t \t \t \t \t <cfoutput query="Languages"> 
 
\t \t \t \t \t \t <option value=#Language#>#Language#</option> 
 
\t \t \t \t \t \t </cfoutput> 
 
\t \t \t \t \t </select> 
 
         
 
         
 
         
 
\t \t \t \t \t <!---<select name="language" class="form-control widthLanguage" id="language" ng-model="searchParam.Language"> 
 
\t \t \t \t \t  <option disabled="disabled" selected="selected" value="">Language</option> 
 
\t \t \t \t \t  <option ng-repeat="l in Languages">{{l.Lang}}</option> 
 
\t \t \t \t  </select>---> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 

 
\t \t \t \t <div class="col-xs-6 no-left-padding"> 
 
\t \t \t \t \t <div class="form-group"><select class="form-control" name="gender" ng-model="searchParam.Gender"> 
 
        <option disabled="disabled" selected="selected" value="">Gender</option> 
 
        <option value=""></option> 
 
        <option>Male</option><option>Female</option> </select></div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t \t 
 
\t \t \t <hr class="hrDoctor" /> 
 
\t \t \t <div style="margin-top:-10px; margin-bottom:10px; text-align:center; font-size:8pt! important">* or Search by Zip code radius *</div> 
 
\t \t \t 
 
\t \t \t <div class="row"> 
 
\t \t \t \t <div class="col-xs-7 no-right-padding"> 
 
\t \t \t \t \t <div class="form-group"> 
 
\t \t \t \t \t \t <div class="input-group"><select class="form-control" name="distance" ng-model="searchParam.distance"><option selected="selected">5</option><option selected="selected">10</option><option selected="selected">15</option><option selected="selected">20</option> </select> 
 

 
\t \t \t \t \t \t \t <div class="input-group-addon">mi</div> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 

 
\t \t \t \t <div class="col-xs-5 no-left-padding widthZip"> 
 
\t \t \t \t \t <div class="form-group"><input allow-pattern="[\d\-]" class="form-control" id="zip" maxlength="5" ng-model="searchParam.Zip" placeholder="Zip code" type="text" /></div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 

 
\t \t \t <div class="form-group"><input class="btn btn-warning btn-block" ng-click="gotoElement('SearchResultsAnchor');" type="submit" value="Search" /></div> 
 
\t \t \t <!---<div class="form-group buttonWidth resetButton"><input class="btn btn-primary btn-block" type="reset" value="Reset" onClick="window.location.reload()"/></div>---> 
 
\t \t </form> 
 
\t <!---</div><!---Progress bar--->---> 
 
\t </div> 
 
</div>

回答

1

您可以使用此代碼:

$(document).ready(function(){ 

      $('#zip').on("input",function(){ 
       $('#city option[value=""]').prop('selected',true); 

      }) 

      $('#city').on("change",function(){ 
       $('#zip').val(""); 
      }) 
     }) 

最終代碼:

<html> 
 
    <head> 
 
    </head> 
 
    <body> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<div class="panel panel-default"> 
 
\t <div class="panel-body"> 
 
<!--- \t <div id="loader" style="position: fixed; top:0; left:0; width:100%; height: 100%; background: url('loader.gif') center center #efefef"></div><!--Progress bar---> 
 
\t \t <form name="providerSearch" ng-submit="SearchProvider(searchParam);" novalidate="" role="form"> 
 
\t \t \t <div class="form-group"><input class="form-control" id="physiciansfirstname" ng-model="searchParam.FirstName" placeholder="First name:" type="text" /></div> 
 

 
\t \t \t <div class="form-group"><input class="form-control" id="physicianslastname" ng-model="searchParam.LastName" placeholder="Last name:" type="text" /></div> 
 

 
\t \t \t <div class="form-group"><select class="form-control" id="providerSpecialty" ng-model="searchParam.Specialty"><option disabled="disabled" selected="selected" value="">Specialty</option> 
 
      <option value=""></option><option>Family practice</option><option>General practice</option><option>Internal medicine</option><option>Pediatrics</option> </select></div> 
 

 
\t \t \t <div class="form-group"> 
 
      <select name="proCity" class="form-control" id="city" placeholder="City" ng-model="searchParam.City"> 
 
\t \t \t \t \t <option disabled="disabled" selected="selected" value="">City</option> 
 
\t \t \t \t \t \t <option value=""></option> 
 
\t \t \t \t \t \t <cfoutput query="cityFind"> 
 
          <option value=#city#>#city#</option> 
 
\t \t \t \t \t \t </cfoutput> 
 
      </select> 
 
         
 
      <!---<select class="form-control" id="city" ng-model="searchParam.City"><option disabled="disabled" selected="selected" value="">City</option><option ng-repeat="c in Cities">{{c.City}}</option> </select>----> 
 
      </div> 
 

 
\t \t \t <div class="row"> 
 
\t \t \t \t <div class="col-xs-6 no-right-padding paddingLanguage"> 
 
\t \t \t \t \t <div class="form-group widthLanguage"> 
 
       
 
         
 
        <select name="language" class="form-control" ng-model="searchParam.Language"> 
 
\t \t \t \t \t  <option disabled="disabled" selected="selected" value="">Language</option> 
 
         <option value=""></option> 
 
\t \t \t \t \t <cfoutput query="Languages"> 
 
\t \t \t \t \t \t <option value=#Language#>#Language#</option> 
 
\t \t \t \t \t \t </cfoutput> 
 
\t \t \t \t \t </select> 
 
         
 
         
 
         
 
\t \t \t \t \t <!---<select name="language" class="form-control widthLanguage" id="language" ng-model="searchParam.Language"> 
 
\t \t \t \t \t  <option disabled="disabled" selected="selected" value="">Language</option> 
 
\t \t \t \t \t  <option ng-repeat="l in Languages">{{l.Lang}}</option> 
 
\t \t \t \t  </select>---> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 

 
\t \t \t \t <div class="col-xs-6 no-left-padding"> 
 
\t \t \t \t \t <div class="form-group"><select class="form-control" name="gender" ng-model="searchParam.Gender"> 
 
        <option disabled="disabled" selected="selected" value="">Gender</option> 
 
        <option value=""></option> 
 
        <option>Male</option><option>Female</option> </select></div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t \t 
 
\t \t \t <hr class="hrDoctor" /> 
 
\t \t \t <div style="margin-top:-10px; margin-bottom:10px; text-align:center; font-size:8pt! important">* or Search by Zip code radius *</div> 
 
\t \t \t 
 
\t \t \t <div class="row"> 
 
\t \t \t \t <div class="col-xs-7 no-right-padding"> 
 
\t \t \t \t \t <div class="form-group"> 
 
\t \t \t \t \t \t <div class="input-group"><select class="form-control" name="distance" ng-model="searchParam.distance"><option selected="selected">5</option><option selected="selected">10</option><option selected="selected">15</option><option selected="selected">20</option> </select> 
 

 
\t \t \t \t \t \t \t <div class="input-group-addon">mi</div> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 

 
\t \t \t \t <div class="col-xs-5 no-left-padding widthZip"> 
 
\t \t \t \t \t <div class="form-group"><input allow-pattern="[\d\-]" class="form-control" id="zip" maxlength="5" ng-model="searchParam.Zip" placeholder="Zip code" type="text" /></div> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 

 
\t \t \t <div class="form-group"><input class="btn btn-warning btn-block" ng-click="gotoElement('SearchResultsAnchor');" type="submit" value="Search" /></div> 
 
\t \t \t <!---<div class="form-group buttonWidth resetButton"><input class="btn btn-primary btn-block" type="reset" value="Reset" onClick="window.location.reload()"/></div>---> 
 
\t \t </form> 
 
\t <!---</div><!---Progress bar--->---> 
 
\t </div> 
 
</div> 
 

 
     <script> 
 
      $(document).ready(function(){ 
 
       
 
       $('#zip').on("input",function(){ 
 
        $('#city option[value=""]').prop('selected',true); 
 
        
 
       }) 
 
       
 
       $('#city').on("change",function(){ 
 
        $('#zip').val(""); 
 
       }) 
 
      }) 
 
     </script> 
 
    </body> 
 
</html>

+0

我仍然在問題中得到相同的結果。我確實有一個問題:你覺得ID「城市」可能是問題嗎? –

+0

@RobertoFlores,當點擊Run code snippet時,結果如何? – Ehsan

+0

@ ehsan:是的,它在工作片段中,但是,當我嘗試在我的結尾做它時,如果我選擇城市,則不允許顯示新結果 –