简体   繁体   English

在Angular JS中提交表单时如何获取输入字段的标签

[英]How to get label of an input field when submit a form in Angular JS

What I want to achieve is when I submit the form it should return the label of the option I choose in the console but it is returning me the value of the option. 我想要实现的是,当我提交表单时,它应该返回我在控制台中选择的选项的标签,但它返回的是选项的值。

Expected Result: DXBA-sky 预期结果:DXBA天空

Result getting: Dubai 结果获得:迪拜

HTML HTML

<div class="suggestField">
    <input type='text' list='listid' placeholder="TO" ng-model="toCity" ng- change="suggestCity(toCity)">
    <datalist id='listid'>
          <option ng-repeat="item in fromSuggestData" label='{{item.PlaceId}}' value='{{item.PlaceName}}'>
    </datalist>
    <button ng-click="submitForm(toCity)">SUBMIT</button>
</div>

JS JS

$scope.fromSuggestData = {
    "PlaceId": "DXBA-sky",
    "PlaceName": "Dubai",
    "CountryId": "AE-sky",
    "RegionId": "",
    "CityId": "DXBA-sky",
    "CountryName": "United Arab Emirates"
}, {
    "PlaceId": "DUJ-sky",
    "PlaceName": "Dubois",
    "CountryId": "US-sky",
    "RegionId": "PA",
    "CityId": "DUJA-sky",
    "CountryName": "United States"
}
$scope.submitForm = function(toCity) {
    console.log(toCity);
}

Here Is demo using ng-option. 这是使用ng-option的演示。

 var app = angular.module("myApp", []); app.controller("myCtrl", function($scope) { $scope.fromSuggestData = [{ "PlaceId": "DXBA-sky", "PlaceName": "Dubai", "CountryId": "AE-sky", "RegionId": "", "CityId": "DXBA-sky", "CountryName": "United Arab Emirates" }, { "PlaceId": "DUJ-sky", "PlaceName": "Dubois", "CountryId": "US-sky", "RegionId": "PA", "CityId": "DUJA-sky", "CountryName": "United States" }]; $scope.submitForm = function(toCity) { console.log(toCity); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl" class="suggestField"> <select id="cityname" ng-model="toCity" ng-options="item as item.PlaceName for item in fromSuggestData"> <option value="">Select</option> </select> <button ng-click="submitForm(toCity.PlaceId)">SUBMIT</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取表单输入值以使用Angular.js替换提交表单 - How to get form input value to replace form on submit with Angular.js 当输入字段是React js中的焦点时如何更改标签颜色 - how to change label color when input field is focus in react js 如何在相同的提交类型输入字段(按钮)中使用onclick(js函数)方法提交表单和处理事件 - How to submit form and handle events using onclick(js function) method in same submit type input field(button) jQuery - 如何在ajax化表单时获取提交类型输入的值 - jQuery - how to get the value of an input of type submit when ajaxifying a form 在表单上按下提交时如何调用不同的get函数; 角 - How to call different get functions when submit is pressed on the form; angular 如何在表单中仅提交 1 个输入字段? - How to submit only 1 input field in form? 如何不使用表格提交单个输入字段 - How to submit individual input field without form 如何在Angular 6中验证提交表单时的输入 - How to validate form input on submit in Angular 6 关注下一个输入不起作用以及用户在最后一个输入字段上时如何提交表单 - Focusing on next input not working and how to submit the form when user on last input field 如何在表单提交中包含文本字段的标签 - how can I include the label of a text field on form submit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM