11

我有一个像电子表格一样布局的表格。

我想验证每个文本框中的文本,如果它不是数字,则更改文本框的背景并显示一条消息。

除了循环部分,我什么都能做。

我猜这是一个 for...each 声明?

4

5 回答 5

15
$('form input[type="text"]').each(function(){
        // Do your magic here
        if (this.value.match(/\D/)) // regular expression for numbers only.
            Error();
});

如果您获得了表单 ID:

$('#formId input[type="text"]').each(function(){
        // Do your magic here
});
于 2012-04-25T20:03:24.440 回答
6
// locate all ":input" elements within a generic <form>,
// then use .each() to loop through all results
$('form :input').each(function(){
  var $el = $(this); // element we're testing

  // attempt to cast the form's value to a number
  var n = parseFloat($el.val());

  // check if the conversion passed
  if (isNaN(n)){
    // $el does not have a number in it
  }
});

我想这就是你所追求的。您还可以指定input[type="text"]是否要更具体<input type="text" ... />

或者,更简洁

$('form input[type="text"]').each(function(i,e){
    if (isNaN(parseFloat(e.value,10))){
        $(e).css('backgroundColor','red');
    }
});
于 2012-04-25T20:04:21.687 回答
2

jsFiddle(http://jsfiddle.net/BctQP/18/

<form id="theForm">
    <input type="text"/>
    <input type="text"/>
    <input type="text"/>
    <input type="text"/>
    <input type="button" onclick="validateTextBoxes();" value="Click"/>
</form>

<script>
    function validateTextBoxes()
    {
        $("#theForm input").each( function()
        {
            if ( $(this).is('[type=text]') && !parseInt( $(this).val() ) )
            {
                $(this).css("background-color","red");
            }
        });
    }
</script>​
于 2012-04-25T20:12:20.737 回答
1

对于每个循环:

for (var $input in $('input[type="text"]')) {
    //code here
}
于 2012-04-25T20:04:10.783 回答
0

得到这个为我工作!:) 参考

使用 jQueryhttps://stackoverflow.com/a/311589遍历表单中的所有文本框

$(document).ready(function() {
		$('form button[type=submit]').click(function() {
			// attach the listener to your button
			debugger;
			var sub_form = $(this.form);
			// use the native JS object with "this"
			$(':input[class="required"]', sub_form).each(function() {
				if (this.value == "") {
					alert("is empty");
				} else {
					alert(this.value);
				}
			});
		});
	});
body {
  margin: 5px;
  padding: 5px;
  border: 1px solid black;
}
#topContainer {
  width: auto;
  margin: 2px;
  padding: 2px;
  border: 1px solid black;
  height: 100px;
}
#mainContainer {
  width: auto;
  height: 200px;
  margin: 2px;
  padding: 2px;
  border: 1px solid black;
}
#footerContainer {
  width: auto;
  height: 200px;
  margin: 2px;
  padding: 2px;
  border: 1px solid black;
}
.gridRow4 {
  margin: 2px 5px;
  width: 25%;
}
.gridRow5 {
  margin: 2px 5px;
  width: ;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <div id="topContainer">
    <h2>text goes here
      </h2>
  </div>
  <div id="mainContainer">
    <form id="form1">
      <select id="input1" class="required" name="input1">
        <option value="volvo">Volvo
        </option>
        <option value="saab">Saab
        </option>
        <option value="mercedes">Mercedes
        </option>
        <option value="audi">Audi
        </option>
      </select>
      <select id="input2" class="required" name="input2">
        <option value="Alpha">Alpha
        </option>
        <option value="Beta">Beta
        </option>
        <option value="Gamma">Gamma
        </option>
        <option value="Radial">Radial
        </option>
      </select>
      <select id="input3" class="required" name="input3">
        <option value="">
        </option>
        <option value="up">up
        </option>
        <option value="down">down
        </option>
        <option value="left">left
        </option>
      </select>
      <input id="input4" class="required" type="text" name="input4" />
      <input id="input5" class="required" type="text" name="input5" />
      <select id="input6" class="required" name="input6">
        <option value="1">1
        </option>
        <option value="2">2
        </option>
        <option value="3">3
        </option>
        <option value="4">4
        </option>
      </select>
      <select id="input7" class="required" name="input7">
        <option value="happy">happy
        </option>
        <option value="sad">sad
        </option>
        <option value="round">round
        </option>
        <option value="flat">flat
        </option>
      </select>
      <button id="btn1" name="btn1" type="submit">Submit
      </button>
    </form>
  </div>
  <div id="footerContainer">
    <form id="form2">
      <input id="inputa" class="required" name="inputa">
      <input id="inputb" class="required" name="inputb">
      <input id="inputc" class="required" name="inputc">
      <input id="inputd" class="required" name="inputd">
      <input id="inpute" class="required" name="inpute">
      <input id="inputf" class="required" name="inputf">
      <input id="inputg" class="required" name="inputg">
      <button id="btn2" name="btn2" type="submit">Submit
      </button>
    </form>
  </div>
</body>

于 2016-11-30T14:33:11.807 回答