通过表中的type =“number”输入迭代

时间:2015-10-15 13:45:49

标签: javascript jquery html5

   <table id="Container1Details" cellspacing="15">


    <tbody>
        <tr>
            <td align="right"> 
                <input name="acidity" type="number" maxlength="4" id="acidity" min="0.112" max="0.152" step="0.001" style="width:108px" required="">
            </td>
        </tr>

        <tr>
            <td align="right">
                <span class="h3"> Soda</span>
                <select name="h2O2Test" id="h2O2Test" allow-empty="true">
                    <option value="">NOT TESTED</option>
                    <option value="N">NEGITIVE</option>
                </select>
            </td>
        </tr>
        <tr>
            <td align="right"> 
                <input name="lacticTest" type="number" maxlength="2" id="lacticTest" min="1" max="20" step="1" style="width:108px" required="">
            </td>
        </tr>
        <tr>    
            <td align='left'>
                <input  name="orderId" size="10pt" type="text" id="orderId"  autocomplete="off"/>
            </td>
        </tr>

    </tbody>
 </table>

是否有可能迭代type =“number”的所有输入并为它们赋值。通过使用类似.find(':text,:file').each(function() {});

之类的东西,我可以轻松地遍历type =“text”和type =“select”的输入

2 个答案:

答案 0 :(得分:4)

是的,你可以,只需使用选择器typenumber类型做同样的事情:

$('input[type="number"]').each(function(){
    //Assign values HERE
    $(this).val('value');
});

希望这有帮助。

答案 1 :(得分:1)

根据您的问题,您似乎想要选择所有<input>元素,而不管其类型属性如何;这会引导我建议:

// selects all <input> elements, and uses the val() method
// to iterate over the returned elements:
$('input').val(function (index, currentValue) {

    // the switch retrieves the type property-value
    // of the current node:
    switch (this.type) {

        // and if it's equal to 'number':
        case 'number':

            // we return the average between the max and min values:
            return (parseFloat(this.max) + parseFloat(this.min)) / 2;
            break;

        // if it's of type equal to 'text':
        case 'text':

            // we return the string of 'appropriateValue':
            return 'appropriateValue';
            break;
    }
});

&#13;
&#13;
$('input').val(function(index, currentValue) {

  switch (this.type) {
    case 'number':
      return (parseFloat(this.max) + parseFloat(this.min)) / 2;
      break;
    case 'text':
      return 'appropriateValue';
      break;
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="Container1Details" cellspacing="15">
  <tbody>
    <tr>
      <td align="right">
        <input name="acidity" type="number" maxlength="4" id="acidity" min="0.112" max="0.152" step="0.001" style="width:108px" required / </td>
    </tr>
    <tr>
      <td align="right"> <span class="h3"> Soda</span>

        <select name="h2O2Test" id="h2O2Test" allow-empty="true">
          <option value="">NOT TESTED</option>
          <option value="N">NEGITIVE</option>
        </select>
      </td>
    </tr>
    <tr>
      <td align="right">
        <input name="lacticTest" type="number" maxlength="2" id="lacticTest" min="1" max="20" step="1" style="width:108px" required="" />
      </td>
    </tr>
    <tr>
      <td align='left'>
        <input name="orderId" size="10pt" type="text" id="orderId" autocomplete="off" />
      </td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

JS Fiddle demo

仅关注<input>type的{​​{1}}个元素,然后:

number