ng内部的大写字母禁用

时间:2013-04-14 12:33:29

标签: angularjs angularjs-directive

我试图通过强制用户在文本输入中键入单词“DELETE”来启用删除按钮。下面的代码有效但它区分大小写,这意味着如果某人例如类型'删除'它将无效。

有人可以建议我如何更改下面的代码以使其不区分大小写吗?

 <div ng-app="">
  <div ng-controller="test">
  <label>Type the word DELETE below to confirm</label>
  <input type="text" placeholder="TYPE DELETE" ng-model="deletetext" /> <br />
  <button class="btn btn-danger" ng-disabled="deletetext != 'DELETE'">DELETE</button>
  </div>
</div>

由于

2 个答案:

答案 0 :(得分:1)

Thilo建议使用deletetext.toUpperCase()!='DELETE'修复此问题。此更正后的代码现在看起来像这样

<div ng-app="">
  <div ng-controller="test">
  <label>Type the word DELETE below to confirm</label>
  <input type="text" placeholder="TYPE DELETE" ng-model="deletetext" /> <br />
  <button class="btn btn-danger" ng-disabled="deletetext.toUpperCase() != 'DELETE'>DELETE</button>
  </div>
</div>

答案 1 :(得分:1)

deletetext.toUpperCase() != 'DELETE'有效。您还可以向deletetext添加$ watch并强制它为大写。它是更好的可用性IMO。

$scope.$watch('deletetext', function(newValue, oldValue){
    $scope.deletetext = newValue.toUpperCase();
});

以下是完整示例: http://jsfiddle.net/GLCAT/