gpt4 book ai didi

javascript - 如何在 AngularJS 中停止事件传播

转载 作者:行者123 更新时间:2023-12-02 17:20:10 25 4
gpt4 key购买 nike

当我单击“注册”按钮时,它也会转到“登录”按钮的处理程序。我已使用 $event.stopPropagation() 作为“注册”按钮来避免它,但它不起作用。

代码 -

<form role="form" ng-submit="login(userName, password)" class="form-horizontal">
<div class="form-group">
<label for="usrNm" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="email" id="usrNm" ng-model="userName" class="form-control input-lg" placeholder="Enter email"/>
</div>
</div>
<div class="form-group">
<label for="pwd" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" id="pwd" ng-model="password" class="form-control input-lg" placeholder="Password"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-default">LOGIN</button>
<button class="btn" ng-click="register(); $event.stopPropagation();">SIGN UP</button>
</div>
</div>
</form>

controller.js -

expmodule.controller('loginForm', function($scope, sharedProperties) {
var auth;
$scope.login = function (userid, password) {
auth = sharedProperties.session.isAuthenticated(userid, password);
if (auth) {
alert("Welcome "+userid);
window.location = "home.html";
} else {
alert("Invalid Login");
}
}
$scope.register = function () {
window.location = "register.html";

}

});

最佳答案

<button> 的默认行为s 是提交它们所属的表单。

将代码更改为:

<button class="btn btn-default">LOGIN</button>
<button type="button" class="btn" ng-click="register();">SIGN UP</button>

关于javascript - 如何在 AngularJS 中停止事件传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24035849/

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