如果必填字段为空,则禁用输入按钮

时间:2018-01-28 18:23:32

标签: javascript

所以我看到最近有关禁用输入按钮的帖子,如果没有填写必填字段。我不是很擅长javascript但是我复制了代码并改为我自己的变量。事情是它不起作用。有人可以帮助我。

问题:如果未填写必填字段,如何禁用输入按钮。一旦填满,它将再次可用“橙色”。

(function() {
  $('form > input').keyup(function() {

    var empty = false;
    $('form > input').each(function() {
      if ($(this).val() == '') {
        empty = true;
      }
    });

    if (empty) {
      $('#register').attr('disabled', 'disabled');
    } else {
      $('#register').removeAttr('disabled');
    }
  });
})()
input[type="submit"][disabled] {
  color: gray;
  border-color: gray;
  background-color: white;
}

.ajax-button {
  display: inline-block;
  width: 100px;
  height: 40px;
  left: 50%;
  top: 50%;
  text-align: center;
}

.submit {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  width: 100px;
  height: 40px;
  border: 2px solid #FFa500;
  border-radius: 10px;
  color: #FFa500;
  font-size: 17px;
  cursor: pointer !important;
  outline: none;
  background-color: white;
}

.submit:hover {
  background-color: orange;
  color: white;
}

.loading {
  font-size: 0;
  width: 30px;
  height: 30px;
  margin-top: 5px;
  border-radius: 15px;
  padding: 0;
  border: 3px solid #FFa500;
  border-bottom: 3px solid rgba(255, 255, 255, 0.0);
  border-left: 3px solid rgba(255, 255, 255, 0.0);
  background-color: transparent !important;
  animation-name: rotateAnimation;
  -webkit-animation-name: wk-rotateAnimation;
  animation-duration: 1s;
  -webkit-animation-duration: 1s;
  animation-delay: 0.2s;
  -webkit-animation-delay: 0.2s;
  animation-iteration-count: infinite;
  -webkit-animation-iteration-count: infinite;
}

@keyframes rotateAnimation {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@-webkit-keyframes wk-rotateAnimation {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}

.fabutton {
  color: #FFa500;
  font-size: 18px !important;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -9px;
  margin-top: -9px;
  -webkit-transform: scaleX(0) !important;
  transform: scaleX(0) !important;
}

.finish {
  -webkit-transform: scaleX(1) !important;
  transform: scaleX(1) !important;
}

.hide-loading {
  opacity: 0;
  -webkit-transform: rotate(0deg) !important;
  transform: rotate(0deg) !important;
  -webkit-transform: scale(0) !important;
  transform: scale(0) !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
  <div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 button-holder" style="margin-top:30px;">
      <div class="ajax-button">
        <div class="fa fa-check done fabutton"></div>
        <div class="fa fa-close failed fabutton"></div>
        <input id="submit" class="submit" type="submit" value="submit" disabled="disabled" />
      </div>
    </div>
  </div>
  </form>
</div>
<script>
</script>

2 个答案:

答案 0 :(得分:0)

Probally $('form > input')返回空。没有<form>标记打开。

之间打开表单标记
<div class="container-fluid">
              <div class="row">

这样:

<div class="container-fluid">
        <form>
              <div class="row">

答案 1 :(得分:0)

试试:

&#13;
&#13;
var app = angular.module("app", []);

app.controller("ctrl", function($scope){
  $scope.options = [
  {name: 'options a'},
  {name: 'options b'}
  ]
  $scope.links = [
     {title: "A"},
     {title: "B"}
 
  ]
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
    <ul>
        <li ng-repeat="i in getDestinations(numberOfDestinations) track by $index">
            {{i.title}}
            <select ng-model="i.selector" ng-options="destination as destination.name for option in destinations"></select>
            
            you select: {{i.selector}}
        </li>
    </ul>
</div>
&#13;
&#13;
&#13;