我有一个像电子表格一样的表格。
我想验证每个文本框中的文本,如果它不是数字,请更改文本框的背景并显示消息。
除了循环部分,我可以做任何事情。
我猜这是......每个声明?
答案 0 :(得分: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
});
答案 1 :(得分:5)
// 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" ... />
或者,more concisely:
$('form input[type="text"]').each(function(i,e){
if (isNaN(parseFloat(e.value,10))){
$(e).css('backgroundColor','red');
}
});
答案 2 :(得分: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>
答案 3 :(得分:1)
对于每个循环:
for (var $input in $('input[type="text"]')) {
//code here
}
答案 4 :(得分:0)
这对我有用! :)参考
Loop through all text boxes in a form using jQuery&amp; https://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>