gpt4 book ai didi

javascript - 根据 AngularJS 的输入更改按钮名称

转载 作者:行者123 更新时间:2023-12-02 14:56:43 24 4
gpt4 key购买 nike

如何更改取决于某些输入是否为空的按钮名称示例:如果 input1 和 input2 不为空,但 input3 为空 - 按钮名称将为“Test”。

这是我的代码,但我不起作用

    <div ng-app="myApp">
<form action="lab2.php" method="POST" ng-controller="controller">
<label>ID</label>
<input type="number" name="id" ng-model="id" name="id"/>
{{id}}
<label>Name:</label>
<input name="name" ng-model="name" type="text"/>
<label>City:</label>
<input name="country" ng-model="country" type="text"/>
<button ng-bind="but" type="submit">{{getName()}}</button>
</form>
</div>
<script>
var app = angular.module('myApp', []);

app.controller('controller', function($scope) {
$scope.but="Operation";
$scope.id=0;
$scope.country="";
$scope.name="";
$scope.getName = function(){
if($scope.id==0 && $scope.name!=="" && $scope.country!=="")
{
return "INSERT";
}
if($scope.id!==0 && $scope.name=="" && $scope.country=="")
{
return "DELETE";
}
if($scope.id!==0 && $scope.name!=="" && $scope.country!=="")
{
return "UPDATE";
}
}
});
</script>

最佳答案

var app = angular.module("app",[]);

app.controller("MyCtrl" , function($scope){

$scope.name1 = "Test";
$scope.name2 = "";
$scope.name3 = "Test3";

$scope.getName = function(){
if( $scope.name1 == "" && $scope.name2 == "" & $scope.name3 !== "")
return "Test";
return "Other";
}

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">

<input type="text" ng-model="name1">
<input type="text" ng-model="name2">
<input type="text" ng-model="name3">

<input type="button" name="{{getName()}}" value="{{getName()}}">


</div>

关于javascript - 根据 AngularJS 的输入更改按钮名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35752807/

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