简体   繁体   English

当值为0时,在数字和范围输入中添加占位符而不是值 - Angular.js

[英]Add placeholder instead of value in number and range input when value is 0 - Angular.js

I have a number and range input that are working in unison using same value using Angular.js value. 我有一个数字和范围输入,使用Angular.js值使用相同的值协同工作。 When the value is set to 0 (which is what it is by default when page loads $scope.lbs_needed = 0; , I want to show a placeholder="0" whenever the value of the number or range input is set to 0 that way the 0 value isn't in front of the user's input without them having to manually delete default 0. 当该值设置为0时(默认情况下,当页面加载$scope.lbs_needed = 0;我想在数字或范围输入的值设置为0时显示placeholder="0"方式0值不在用户输入的前面而不必手动删除默认值0。

Here's my html: 这是我的HTML:

<form>
    <div class="form-group">
        <label for="number">Pounds of nitrogen desired per acre:</label>
        <input type="number" class="form-control number-input" id="number" ng-model="lbs_needed" ng-change="calculate2()" value="{[{lbs_needed}]}" min="0" max="500" value="{[{lbs_needed}]}" placeholder="0">
        <input type="range" min="0" max="500" class="form-control" id="number" ng-model="lbs_needed" ng-change="calculate2()" value="{[{lbs_needed}]}" placeholder="0">
    </div>
</form>

So, if I understand correctly you want the textbox to have a default value but also want it to be changeable instantly without users having to backspace over the default? 因此,如果我理解正确,您希望文本框具有默认值,但也希望它可以立即更改,而无需用户退出默认值?

Try this: 尝试这个:

 var input = document.getElementById('change-default'); input.onfocus = function () { if (input.value == 0) { input.value = ''; } } input.onblur = function () { if (input.value == '') { input.value = '0'; } } 
 <input type="number" placeholder="0" value="0" id="change-default"> 

What I'm doing is listening for an focus event, you could also use addEventListener instead of onfocus . 我正在做的是监听focus事件,你也可以使用addEventListener而不是onfocus

If the value of the input box is 0 you remove its value ready for the user to type theirs in. 如果输入框的值为0 ,则删除其值,以供用户键入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM