admin管理员组文章数量:1425738
I want to enable a button only when a text is entered on an input field. So far I have this code on my app.js
.controller("EnableDisable", function(){
$scope.editableInput = false;
})
.directive("inputDisabled", function(){
return function(scope, element, attrs){
scope.$watch(attrs.inputDisabled, function(val){
if(val)
element.removeAttr("disabled");
else
element.attr("disabled", "disabled");
});
}
});
This is the view implementation :
<div ng-controller="EnableDisable">
<input type="text" input-disabled="editableInput" />
<button ng-click="editableInput = !editableInput">enable/disable</button>
</div>
This is the plunk url: Presently when I launch the app, the button is not disabled by default. Please with the above challenge how can I make the button clickable (receive events)when a text is entered on the input field. Kindly assist!
I want to enable a button only when a text is entered on an input field. So far I have this code on my app.js
.controller("EnableDisable", function(){
$scope.editableInput = false;
})
.directive("inputDisabled", function(){
return function(scope, element, attrs){
scope.$watch(attrs.inputDisabled, function(val){
if(val)
element.removeAttr("disabled");
else
element.attr("disabled", "disabled");
});
}
});
This is the view implementation :
<div ng-controller="EnableDisable">
<input type="text" input-disabled="editableInput" />
<button ng-click="editableInput = !editableInput">enable/disable</button>
</div>
This is the plunk url: https://plnkr.co/edit/bHtZJ1H5JvM2XTuhft6J?p=preview Presently when I launch the app, the button is not disabled by default. Please with the above challenge how can I make the button clickable (receive events)when a text is entered on the input field. Kindly assist!
Share Improve this question edited Feb 4, 2016 at 12:01 user57508 asked Feb 4, 2016 at 11:22 BlazeBlaze 2,34912 gold badges44 silver badges85 bronze badges 7-
just a question.. Why are you not using
form
validations – Minato Commented Feb 4, 2016 at 11:27 - please create JSFiddle of your code – Amit Ramoliya Commented Feb 4, 2016 at 11:28
- what do you mean trigger the button? – Navoneel Talukdar Commented Feb 4, 2016 at 11:34
- Here is plnk I have made: plnkr.co/edit/bHtZJ1H5JvM2XTuhft6J?p=preview – Blaze Commented Feb 4, 2016 at 11:36
- Why you want do it using directory?Other not acceptable? – RIYAJ KHAN Commented Feb 4, 2016 at 11:46
3 Answers
Reset to default 2you can do this with Angular.
<input type="text" ng-model="textEntered" />
<button ng-disabled="!textEntered">Continue</button>
Try this one
controller("EnableDisable", function($scope){
$scope.textEntered = "";
});
HTML :
<button ng-disabled="!textEntered">enable/disable</button>
Please refer Plunker
You will have to set initial scope value like this.Note that I have set it false so condition will negate and make button disable on page load.Then I have set a watch on input value accordingly toggled the scope value.
var app = angular.module('plunker', []);
app.controller("EnableDisable", function(){
$scope.textEntered=false;
$scope.$watch($scope.textEntered,function(v)
{
if(v)
{
$scope.textEntered=true;
}
else
{
$scope.textEntered=false;
}
}
);
});
and html is -
<input type="text" ng-model="textEntered" />
<button ng-disabled="!textEntered">Continue</button>
https://plnkr.co/edit/o1Im8MSXbEa8kyqPqBB9?p=preview
本文标签: javascriptEnable a button on entering text in input fieldStack Overflow
版权声明:本文标题:javascript - Enable a button on entering text in input field - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745367953a2655605.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论