如何使文本字段不可编辑

时间:2016-07-04 06:35:21

标签: angularjs uidatepicker

我有文本字段,它使用datepicker。我希望文本字段不可编辑,因此我们只能使用datepicker进行编辑。

socket_sendto

任何帮助表示感谢。

5 个答案:

答案 0 :(得分:1)

我设法使用JavaScript修复它,我将焦点放在下面的日历检查中:plunker

您也可以拨打按钮.click直接在同一功能中打开日历

<script>
  function RejectEnter()
  {
    document.getElementById("btnCal").focus();
   document.getElementById("btnCal").click();
    return false;
    }

和HTML:

 <input type="text" onfocus="return RejectEnter();" class="form-control" ng-model="dt" uib-datepicker-popup  is-open="popupDateFrom.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close"/>
          <span class="input-group-btn">
            <button type="button" id="btnCal" class="btn btn-default" ng-click="open()"><i class="glyphicon glyphicon-calendar"></i></button>
          </span>

答案 1 :(得分:1)

只需使用 readonly 标记输入标记即可实现此目的。

&#13;
&#13;
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function ($scope) {
 $scope.inlineOptions = {
    customClass: getDayClass,
    minDate: new Date(),
    showWeeks: true
  };

  $scope.dateOptions = {
    maxDate: new Date(2020, 5, 22),
    minDate: new Date(),
    startingDay: 1
  };


  $scope.toggleMin = function() {
    $scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null : new Date();
    $scope.dateOptions.minDate = $scope.inlineOptions.minDate;
  };

  $scope.toggleMin();

  $scope.open = function() {
    $scope.popupDateFrom.opened = true;
  };
  
  $scope.open1 = function() {
    $scope.popupDateTo.opened = true;
  };
  
  $scope.popupDateFrom = {
    opened: false
  };
  
  $scope.popupDateTo = {
    opened: false
  };


  function getDayClass(data) {
    var date = data.date,
      mode = data.mode;
    if (mode === 'day') {
      var dayToCheck = new Date(date).setHours(0,0,0,0);
    }

    return '';
  }
 
});
&#13;
  .full button span {
    background-color: limegreen;
    border-radius: 32px;
    color: black;
  }
  .partially button span {
    background-color: orange;
    border-radius: 32px;
    color: black;
  }
&#13;
<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.3.3.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<div ng-controller="DatepickerDemoCtrl">
    <pre>Selected date is: <em>{{dt|date:'yyyy-MM-dd' }}</em></pre>

<div class="row">
      <div class="col-md-6">
        <p class="input-group">
          <input type="text" readonly class="form-control" ng-model="dt" uib-datepicker-popup  is-open="popupDateFrom.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close"/>
          <span class="input-group-btn">
            <button type="button" class="btn btn-default" ng-click="open()"><i class="glyphicon glyphicon-calendar"></i></button>
          </span>
        </p>
      </div>
      <div class="col-md-6">
        <p class="input-group">
          <input type="text" readonly class="form-control" ng-model="dt1" uib-datepicker-popup  is-open="popupDateTo.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close"/>
          <span class="input-group-btn">
            <button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
          </span>
        </p>
      </div>
  </div>

    <hr />
</div>
  </body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

只需将输入框设为只读是最简单的解决方案,我已经尝试过你的plunker,只需输入 readonly 输入属性,它可以正常工作。

请看下面的一行,

<input type="text" class="form-control" ng-model="dt" uib-datepicker-popup  is-open="popupDateFrom.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close"/>

只读地,上面的行将改为此,

<input type="text" readonly class="form-control" ng-model="dt" uib-datepicker-popup  is-open="popupDateFrom.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close"/>

快乐的编码。

答案 3 :(得分:0)

您可以将输入标记为已禁用。用户将无法单击它来编辑其原始值,但他们可以打开日期选择器。

另一个方法是在用户点击输入时打开日期选择器。

答案 4 :(得分:0)

只需要使用javascript覆盖输入控件上的键事件以禁止手动输入。

<input onkeydown="return false" ...... />