验证两个输入

时间:2017-04-26 11:54:10

标签: javascript jquery input

我正在开发一个应用程序,因为我在验证两个输入到Sub FindLastRowInExcelTableColAandB() Dim lastRow1 As Long, lastRow2 As Long Dim ws As Worksheet Set ws = Sheets("DataÖnskemål") 'Assuming the name of the table is "Table1" lastRow1 = ws.ListObjects("Table1").Range.Columns(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row lastRow2 = ws.ListObjects("Table1").Range.Columns(2).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row End Sub 按钮时遇到了困难。



removeClass

$('input#firstInput').blur(function(){
    tmpval2 = $('input#secondInput').val();
    tmpval = $(this).val();
	
    if(tmpval == '') {
        $("#myButton").addClass('disabled')
    }
    else if(tmpval2 == ''){
        $("#myButton").addClass('disabled')
    } else {
        $("#myButton").removeClass('disabled')
    }
});




这是我为此开发的代码。

Fiddle Link

5 个答案:

答案 0 :(得分:1)

您可能正在侦听按键,然后检查第一个输入或第二个输入的值是否为空。如果是,则禁用该按钮。否则,两个输入都有值,因此可以删除已禁用的类。

对HTML的唯一更改是我已将类input-field添加到应检查的所有输入字段中。

$('.input-field').keyup(function() {
    var firstValue = $('#firstInput').val(),
        secondValue = $('#secondInput').val();

     if (firstValue == '' || secondValue == '') {
         $("#myButton").addClass('disabled');
     } else {
         $("#myButton").removeClass('disabled');
     }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<hr>
<div class="container">
  <div class="row">
    <div class="col-md-12">

      <div class="panel panel-default">
        <div class="panel-heading">
          <h3 class="panel-title"> Validate Two Inputs</h3>
        </div>
        <div class="panel-body">
          <input type="text" class="input-field" id="firstInput" placeholder="First Value" />
          <br><br>
          <input type="text" class="input-field" id="secondInput" placeholder="Second Value" />
          <br><br>
          <button id="myButton" class="btn btn-success disabled">Send</button>
        </div>
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:1)

尝试此代码或通过以下小提琴链接

here

JAVASCRIPT代码 -

ListView

答案 2 :(得分:0)

首先删除类,然后添加它,一次检查两个输入

$('.inputs').change(function() {
  $("#myButton").removeClass('disabled');

  var isValid = false;

  $('.inputs').each(function() {
    if ($(this).val() && $(this).val().length) {
      isValid = true;

      return false; // same as break;
    }
  });

  if (!isValid) {
    $("#myButton").addClass('disabled')
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<hr>
<div class="container">
  <div class="row">
    <div class="col-md-12">

      <div class="panel panel-default">
        <div class="panel-heading">
          <h3 class="panel-title"> Validate Two Inputs</h3>
        </div>


        <div class="panel-body">
          <input type="text" class="inputs" placeholder="First Value" />
          <br><br>
          <input type="text" class="inputs" placeholder="Second Value" />
          <br><br>
          <button id="myButton" class="btn btn-success disabled"> Send
      </button>
        </div>


      </div>


    </div>
  </div>
</div>

答案 3 :(得分:0)

您需要在变更事件中执行此操作。 有我的解决方案 - 快速:)

示例:

$('input.required').keyup(function(){

/* Validate all required fields */
var err = 0;
$('input.required').each(function(){
    var input = $(this);
  if(input.val().length == 0){
    err++;
  }
});

if(!err){
    $('#myButton').removeClass('disabled');
}
});

https://jsfiddle.net/kgynth5x/11/

答案 4 :(得分:0)

检查这是否可以解决您的问题

谢谢

&#13;
&#13;
$('input#firstInput').keyup(function(){
		var tmpval = $(this).val();
    var tmpval1 = "";
    $('input#secondInput').keyup(function(){
       tmpval1 = $(this).val(); 
       checkValid(tmpval,tmpval1);
    });
    checkValid(tmpval,tmpval1);
});

function checkValid(tmpval,tmpval1){
  if(tmpval == '' || tmpval1 == '') {
	        $("#myButton").addClass('disabled')
	}
  else {
      $("#myButton").removeClass('disabled')
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<hr>
<div class="container">
  <div class="row">
  <div class="col-md-12">
  
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"> Validate Two Inputs</h3>
      </div>
  
  
    <div class="panel-body">
      <input type="text" id="firstInput" placeholder="First Value"  />
      <br><br>
      <input type="text" id="secondInput" placeholder="Second Value" />
      <br><br>
      <button id="myButton" class="btn btn-success disabled"> Send
      </button>
    </div>
    
    
    </div>
    
    
    </div>
  </div>
</div>
&#13;
&#13;
&#13;