[英]HTML Select field is not selecting the first value by default
我正在使用 angular js 和 HTML 创建一些基本网站,并且由于某种原因我有一个包含 3 个选项的下拉菜单,默认情况下未选择选项字段中的第一个值。 浏览器上的 HTML 页面显示空白选项(即使我的 select 列表中没有空白选项);
HTML 在浏览器上:
HTML 代码:
<div class="col-md-4">
<select id="eventtype2" ng-model="formAdata.eventtype2" class="form-control">
<option class="dropdown-item" value="ordinaryevent">Ordinary Event</option>
<option class="dropdown-item" value="errordeclaration"> Error Declaration </option>
</select>
</div>
我的要求是在页面加载的字段中选择第一个选项Ordinary Event
。 我尝试了selected
的选项,但仍然没有运气。 在页面加载的几分之一秒内,该字段显示值Ordinary Event
,但随后消失。 我并没有试图将它隐藏在 angular 或其他不确定原因中。
我也尝试过以下但没有运气:
<div class="col-md-4">
<select id="eventtype2" ng-model="formAdata.eventtype2" class="form-control">
<option class="dropdown-item" value="ordinaryevent" selected>Ordinary Event</option>
<option class="dropdown-item" value="errordeclaration"> Error Declaration </option>
</select>
我尝试将以下命令添加到我的 angular:
$scope.formAdata.eventtype2 = 'ordinaryevent';
但是由于某种原因添加此命令后,我从 node.js 填充的其他下拉列表变得混乱:
<div class="row">
<div class="col-md-12 form-group">
<label for="eventtype1" class="col-md-3 control-label">Event Type</label>
<div class="col-md-4">
<select id="eventtype1" ng-model="formAdata.eventtype1" class="form-control" ng-options='eventtype1 for eventtype1 in eventType'>
<option class="dropdown-item" value="" disabled selected>Select Event Type</option>
<option class="dropdown-item" value="{{eventtype1}}"> {{ eventtype1 }} </option>
</select>
</div>
<div class="col-md-4">
<select id="eventtype2" ng-model="formAdata.eventtype2" class="form-control">
<option class="dropdown-item" value="ordinaryevent" selected>Ordinary Event</option>
<option class="dropdown-item" value="errordeclaration"> Error Declaration </option>
</select>
</div>
</div>
app.controller('AppController', function($scope, $http) {
$scope.formAdata.eventtype2 = 'ordinaryevent';
$scope.createEvents = function(){
$http({
url: "/createEvents",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param($scope.formAdata)
}).success(function(response) {
$scope.xmldata = response[0].XML;
$scope.jsondata = JSON.stringify(JSON.parse(response[1].JSON), undefined, 4);
}).error(function(error) {
console.log(error)
});
}
$scope.init = function () {
$http({
url: "/populateFields",
method: "GET"
}).success(function(response) {
$scope.businessSteps = response.businessStep;
$scope.eventType = response.eventType;
}).error(function(error) {
console.log(error);
});
};
});
您可以将 controller 中formAdata.eventtype2
model 的默认值设置为您想要的默认值
例如
你可以在你的 controller
$scope.formAdata.eventtype2 = 'ordinaryevent';
或您对第一个选项的任何价值
对于第二个问题,试试这个
<div class="col-md-4" ng-if="eventType">
<select id="eventtype1" ng-model="formAdata.eventtype1" class="form-control" ng-options='eventtype1 for eventtype1 in eventType'>
<option class="dropdown-item" value="" disabled selected>Select Event Type</option>
<option class="dropdown-item" value="{{eventtype1}}"> {{ eventtype1 }} </option>
</select>
</div>
app.controller('AppController', function($scope, $http) {
$scope.formAdata = {
eventtype1: '',
eventtype2: 'ordinaryevent',
};
$scope.createEvents = function(){
$http({
url: "/createEvents",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param($scope.formAdata)
}).success(function(response) {
$scope.xmldata = response[0].XML;
$scope.jsondata = JSON.stringify(JSON.parse(response[1].JSON), undefined, 4);
}).error(function(error) {
console.log(error)
});
}
$scope.init = function () {
$http({
url: "/populateFields",
method: "GET"
}).success(function(response) {
$scope.businessSteps = response.businessStep;
$scope.eventType = response.eventType;
}).error(function(error) {
console.log(error);
});
};
$scope.init();
您必须在默认选项中添加“选定”
<option value="xx" selected>...
使用选定属性
<div class="col-md-4"> <select id="eventtype2" ng-model="formAdata.eventtype2" class="form-control"> <option class="dropdown-item" selected value="ordinaryevent">Ordinary Event</option> <option class="dropdown-item" value="errordeclaration"> Error Declaration </option> </select> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.