Loop through all text boxes in a form using jQuery

后端 未结 5 772
予麋鹿
予麋鹿 2020-12-16 18:01

I have a form which is laid out like a spreadsheet.

I want to validate the text in each textbox and if it\'s not numeric, change the background of the textbox and di

相关标签:
5条回答
  • 2020-12-16 18:39

    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>​
    
    0 讨论(0)
  • 2020-12-16 18:49
    // 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
      }
    });
    

    I think is what you're after. You can also specify input[type="text"] if you want to be more specific to <input type="text" ... />

    Or, more concisely:

    $('form input[type="text"]').each(function(i,e){
        if (isNaN(parseFloat(e.value,10))){
            $(e).css('backgroundColor','red');
        }
    });
    
    0 讨论(0)
  • 2020-12-16 18:49

    For each loop:

    for (var $input in $('input[type="text"]')) {
        //code here
    }
    
    0 讨论(0)
  • 2020-12-16 18:57

    Got this working for me! :) references

    Loop through all text boxes in a form using jQuery & 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>

    0 讨论(0)
  • 2020-12-16 19:01
    $('form input[type="text"]').each(function(){
            // Do your magic here
            if (this.value.match(/\D/)) // regular expression for numbers only.
                Error();
    });
    

    If you got the form id:

    $('#formId input[type="text"]').each(function(){
            // Do your magic here
    });
    
    0 讨论(0)
提交回复
热议问题