gpt4 book ai didi

javascript - 如何从angularjs中的表单中查找名字和姓氏?

转载 作者:行者123 更新时间:2023-12-03 08:51:56 26 4
gpt4 key购买 nike

谁能告诉我如何从表单字段中检索名字和姓氏的值。条件是名字和姓氏写在同一个文本字段中,并用 ""(空格)分隔。我需要得到它的值(value)。

这是我的代码和条件 enter image description here

PLUNKR

<body ng-app="app">
<div ng-controller="first">
<form name="myform" novalidate>
<div>
<input type="text" placeholder="FirstName lastname" ng-model="user.firstname" name="username" required style="width: 300px;padding: 20px">

<!--<span ng-show="myform.username.$dirty && myform.username.$error.required">Required</span>-->

</div>

<div>
<input type="email" placeholder="Email" ng-model="user.email" name="email" required style="width: 300px;padding: 20px;margin-top: 50px">
</div>
<p>{{user}}</p>
</form>
</div>
</body>

最佳答案

http://plnkr.co/edit/QkECAMaNJf2ACXo2LJpe?p=preview

angular.module('app', ['ionic']).controller('first',function($scope){
$scope.user = {};

$scope.nameChanged = function() {
if ($scope.name) {
var name = $scope.name.split(" ");

if (name.length > 0) {
$scope.user.firstname = name[0]; // first name;
if(name.length>1) {
$scope.user.lastname = name[1]; // last name
} else {
$scope.user.lastname = '';
}
}
} else {
$scope.user.firstname = '';
$scope.user.lastname = '';
}
}
})

和 html

    <input type="text" placeholder="FirstName lastname" ng-model="name" ng-change="nameChanged()" name="username" required style="width: 300px;padding: 20px">

关于javascript - 如何从angularjs中的表单中查找名字和姓氏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32643089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com