在下拉列表周围创建边框

时间:2017-01-26 00:03:57

标签: javascript jquery

问题:如果用户输入邮政编码时留空,我想在下拉框周围创建边框。否则,保持原样。

目前,它是如何工作的,如果用户输入邮政编码并选择提交,则会弹出警报,并且在用户选择里程之前不会显示结果。仅当用户选择使用邮政编码查找位置时才会出现这种情况。否则,它将填充结果。

以下是表格和脚本:



$(document).ready(function(){
		var $zip = $('#zip');
		var $city = $('#city');
		var $hospital = $('#hospital');
		var $miles = $('#miles');

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

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

		$hospital.on("change",function(){
			$zip.val('').trigger('input');
			$miles.val('').trigger('input');
		});
		$zip.one("change", function(){
			$miles.val('').trigger('input');
		});
	});
	function checkTextField() {
		var distance = document.forms["UrgentCareSearch"]["distance"].value;
		var zip = document.forms["UrgentCareSearch"]["zip"].value;
	
		if(zip && !distance){
			var alertMessage = "Please Select Distance When You Are Entering A Zip Code.";
			alert(alertMessage);
			return false; //Does not submit form
		}
		else
			return true;
	}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<div class="panel panel-default">
	<div class="panel-body">
		<form name="UrgentCareSearch" ng-submit="SearchUrgentCare(searchParam);" novalidate role="form">
			<div class="form-group"><input class="form-control" id="hospital" ng-model="searchParam.HospitalName" placeholder="Hospital Name" type="text" /></div>

			<div class="form-group">
            <select class="form-control" id="city" ng-model="searchParam.City">
            <option disabled="disabled" selected="selected" value="">City</option>  
            <option value=""></option>
						  <cfoutput query="HospCityFind">
						  <option value=#officecity#>#officecity#</option>
						</cfoutput> 
					  </select></div>

			<hr />
			<div style="margin-top:-10px; margin-bottom:10px; text-align:center; font-size:8pt! important">* or Search by Zip code radius *</div>

			<div class="row">
				<div class="col-xs-7 no-right-padding">
					<div class="form-group">
						<div class="input-group"><!---<select class="form-control" name="distance" ng-model="searchParam.Distance" ng-options="mile.value for mile in miles"></select>--->
						<select class="form-control" id="miles" name="distance" ng-model="searchParam.Distance">
							<option value={{v.value}} ng-repeat="(k , v) in miles track by $index">{{v.value}}</option>
						</select>
							<div class="input-group-addon">miles</div>
						</div>
					</div>
				</div>

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

			<div class="form-group"><input class="btn btn-warning btn-block" onclick="return checkTextField();" ng-click="gotoElement('SearchResultsAnchor');" type="submit" value="Search" /></div>
		</form>
	</div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

好的,这应该有效。 修改了checkTextField()函数以突出显示里程字段(如果为空)

  

document.getElementById(“miles”)。style.border =“solid#000000”

另外,您的里程Dropbox也有oninput标签

  

oninput =“unHighlight()”   其中调用非突出显示功能来删除该框,以防除“”之外的任何值。

参考:http://www.w3schools.com/jsref/event_oninput.asp            http://www.w3schools.com/js/js_htmldom_eventlistener.asp

$(document).ready(function() {
  var $zip = $('#zip');
  var $city = $('#city');
  var $hospital = $('#hospital');
  var $miles = $('#miles');

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

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

  $hospital.on("change", function() {
    $zip.val('').trigger('input');
    $miles.val('').trigger('input');
  });
  $zip.one("change", function() {
    $miles.val('').trigger('input');
  });
});

function checkTextField() {
  var distance = document.forms["UrgentCareSearch"]["distance"].value;
  var zip = document.forms["UrgentCareSearch"]["zip"].value;

  if (zip && !distance) {
    var alertMessage = "Please Select Distance When You Are Entering A Zip Code.";
      document.getElementById("miles").style.border = " solid #000000"
    alert(alertMessage);
    return false; //Does not submit form
  } else
    return true;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<div class="panel panel-default">
  <div class="panel-body">
    <form name="UrgentCareSearch" ng-submit="SearchUrgentCare(searchParam);" novalidate role="form">
      <div class="form-group">
        <input class="form-control" id="hospital" ng-model="searchParam.HospitalName" placeholder="Hospital Name" type="text" />
      </div>

      <div class="form-group">
        <select class="form-control" id="city" ng-model="searchParam.City">
          <option disabled="disabled" selected="selected" value="">City</option>
          <option value=""></option>
          <cfoutput query="HospCityFind">
            <option value=#officecity#>#officecity#</option>
          </cfoutput>
        </select>
      </div>

      <hr />
      <div style="margin-top:-10px; margin-bottom:10px; text-align:center; font-size:8pt! important">* or Search by Zip code radius *</div>

      <div class="row">
        <div class="col-xs-7 no-right-padding">
          <div class="form-group">
            <div class="input-group">
              <!---<select class="form-control" name="distance" ng-model="searchParam.Distance" ng-options="mile.value for mile in miles"></select>--->
              <select class="form-control" id="miles" name="distance" ng-model="searchParam.Distance" oninput = "unHighlight()">
                <option value={{v.value}} ng-repeat="(k , v) in miles track by $index">{{v.value}}</option>
              </select>
              <div class="input-group-addon">miles</div>
            </div>
          </div>
        </div>

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

      <div class="form-group">
        <input class="btn btn-warning btn-block" onclick="return checkTextField();" ng-click="gotoElement('SearchResultsAnchor');" type="submit" value="Search" />
      </div>
      <script>
       
        function unHighlight() {
         
          var miles =  document.getElementById("miles").value;
                      
         try{
        
            if(miles != ""){
         document.getElementById("miles").style.border = "none"
          }
        }
           catch(err)
             {
                          document.getElementById("miles").style.border = "none"
               }
         
                    }
      </script>
      
    </form>
  </div>
</div>