【问题标题】:HTML Select field is not selecting the first value by defaultHTML Select 字段默认不选择第一个值
【发布时间】:2020-09-28 16:52:41
【问题描述】:

我正在使用 Angular js 和 HTML 创建一些基本网站,并且出于某种原因,我有一个包含 3 个选项的下拉菜单,默认情况下未选择选项字段中的第一个值。浏览器上的 HTML 页面显示空白选项(即使我的选择列表中没有空白选项);

浏览器上的 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,但随后消失。我不是想把它隐藏在角度或不知道为什么的地方。

我也尝试了以下但没有运气:

<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>

我尝试将以下命令添加到我的角度:

$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);
        });
    };
});

【问题讨论】:

    标签: html select bootstrap-4 dropdown html5-history


    【解决方案1】:

    您可以将控制器中formAdata.eventtype2模型的默认值设置为您想要的默认值

    例如

    你可以在你的控制器中拥有它

    $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();
    

    【讨论】:

    • 感谢这个工作正常:$scope.formAdata.eventtype2 = 'ordinaryevent';
    • 但这会影响我使用 angularjs 填充的其他下拉列表和我从 nodejs 模型获得的列表。以前其他下拉列表已正确填充值,但添加此命令后,没有填充任何下拉字段,它只显示{{ variable name }}
    • 说实话不确定你的意思,你能详细说明一下吗?以前有什么用,它是怎么坏的?
    • 基本上我有 2 个带有 eventtype1 和 eventtype2 的下拉菜单。 eventtype2 直接在带有选项的 HTML 中填充,但是 eventtype1 列表非常大,所以我将选项存储在 nodejs 模型中,并且我在角度调用 nodejs 函数来填充它。在添加命令之前,您提供的 eventtype1 工作正常,但 eventtype2 没有选择第一个值,但在添加命令后 eventtype2 工作正常,但 eventtype1 由于某种原因被搞砸了,现在没有被填充。我会将代码添加到我的主要问题中。
    • 我仍然不确定eventtype1 来自哪里,因为它不在您的任何地方。但我怀疑你只需要将$scope.formAdata.eventtype2 = 'ordinaryevent'; 更改为$scope.formAdata = {eventtype1: '', eventtype2: 'ordinaryevent'} 可能
    【解决方案2】:

    您必须在默认选项中添加“已选择”

    <option value="xx" selected>...
    

    【讨论】:

    • 我已经尝试过了,但它似乎对我不起作用。尽管在初始加载时会显示选项,但它会立即消失,但我不确定这些选项为什么以及如何消失。如果单击该字段,则可以看到该选项,但我希望默认情况下应选择第一个选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 2021-12-11
    • 2014-04-11
    相关资源
    最近更新 更多