简体   繁体   English

动态更改输入字段的占位符?

[英]Dynamically change an input field's placeholder?

I am working with Javascript and the d3 library, and AngularJS.我正在使用 Javascript 和 d3 库以及 AngularJS。

Is there a way to dynamically change an input box's placeholder?有没有办法动态更改输入框的占位符? I am working with a calendar widget and multiple views and wanted to see if there was a way to have the placeholder always be the value that was last inputted into the field.我正在使用日历小部件和多个视图,想看看是否有办法让占位符始终是最后输入到字段中的值。

I wrote a small function that always returns the last thing that was entered into the input field...but then when I tried setting placeholder=functionIwrote() it literally makes the placeholder "fucntionIwrote()" instead of running the function.我写了一个小函数,它总是返回输入字段的最后一个东西……但是当我尝试设置 placeholder=functionIwrote() 时,它实际上使占位符“fucntionIwrote()”而不是运行该函数。

I can't comment but... If you're using angularJs, you just have to bind the model of your input to the placeholder!我无法发表评论,但是...如果您使用 angularJs,您只需将输入模型绑定到占位符即可!

<input type="date" placeholder="myModel" ng-model="myModel" />

This way, your input would always be the latest filled value.这样,您的输入将始终是最新的填充值。

If you want to change your view, and then retrieve datas, then you have to store them outside of the controller scope - which is "cleaned" every time if you're using the ngIf directive -.如果你想改变你的视图,然后检索数据,那么你必须将它们存储在控制器范围之外——如果你使用 ngIf 指令,每次都会“清理”它。

A good way to do this is to use a service as persistance layer.这样做的一个好方法是使用服务作为持久层。

Since you want the placeholder to be changed or updated both automatically and dynamically , you may use the jQuery code below:由于您希望automaticallydynamically更改或更新placeholder ,您可以使用下面的 jQuery 代码:

$(function() {
    $('input').on('change blur', function() {
        !this.value || $(this).attr('placeholder', this.value);
    });
});

WORKING JS FIDDLE DEMO工作 JS 小提琴演示

Yes you can do it in this way ,是的,你可以这样做,

<input 
placeholder="Select Date" 
onfocus="(this.type='date')" 
onblur="if (!this.value) this.type = 'text'">

here's a fiddle: http://jsfiddle.net/bBh3L/这是一个小提琴: http : //jsfiddle.net/bBh3L/

'placeholder' is an element attribute, so you can use $('#myInput').attr(attributeName, attributeValue) to set it. 'placeholder' 是一个元素属性,所以你可以使用 $('#myInput').attr(attributeName, attributeValue) 来设置它。

In this case, I mapped the button click to change the placeholder using:在这种情况下,我映射了按钮单击以使用以下方法更改占位符:

$('#myInput').attr('placeholder', 'new one');

I guess that you're trying to wrap your calendar widget into an angular directive, if so, here's what I did for a similar use case (I display the accepted/valid format as placeholder):我猜你正试图将你的日历小部件包装成一个角度指令,如果是这样,这是我为类似用例所做的(我将接受/有效格式显示为占位符):

module.directive('yourDirective', [function() {
  return {
    link: function($scope, $element, $attrs, $controller) {

      // bind 'yourValue' (the one you want to show as placeholder) in the scope
      $scope.$watch('yourValue', function(value) {
        $attrs.$set('placeholder', value);
      });
    }
  };
}]);

There exists a conditional attribute property in AngularJS ng-attr-{property_name} AngularJS ng-attr-{property_name}存在一个条件属性属性

For example, I'm using different placeholders for different search options using例如,我使用不同的占位符用于不同的搜索选项

 ng-attr-placeholder="{{isAdvanceSearch ? setPlaceholder(searchOption) : 'John Smith, 08/23/1970, 123'}}"

Here on the basis of isAdvanceSearch variable, I'm setting different placeholders in setPlaceholder method.这里基于isAdvanceSearch变量,我在setPlaceholder方法中设置了不同的占位符。

setPlaceholder method returns the placeholder to set in the input field. setPlaceholder方法返回要在输入字段中设置的占位符。

$scope.setPlaceholder = function(searchOption) {
     if (searchOption === "foo") {
          return "Search by foo… e.g. foo1";
     } else if (searchOption === "bar") {
          return "Search by bar… e.g. bar123";
     } else {
          return "John Smith, 08/23/1970, 123";
     }
};

Note: John Smith, 08/23/1970, 123 is the default placeholder.注意: John Smith, 08/23/1970, 123是默认占位符。 Don't forget to wrap the expression in the {{}} brackets.不要忘记将表达式括在{{}}方括号中。

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

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