遍历type="number"在一张桌子上
Iterate through input of type="number" in a table
<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
对类型number
做同样的事情:
$('input[type="number"]').each(function(){
//Assign values HERE
$(this).val('value');
});
从你的问题看来,你想选择所有的<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;
}
});
$('input').val(function(index, currentValue) {
switch (this.type) {
case 'number':
return (parseFloat(this.max) + parseFloat(this.min)) / 2;
break;
case 'text':
return 'appropriateValue';
break;
}
});
<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>
JS Fiddle demo.
只关注那些type
是number
的<input>
元素,则:
$('input[type=number]').val(function (index, currentValue) {
// sets the new value of each number-<input> to the
// result of the parsed current-value multiplied by
// the index of the current element in the collection
// returned by the selector:
return parseInt(currentValue, 10) * index;
});
相关文章:
- 有人能提供一张ember数据与broswer's的持久层
- jQuery只隐藏<tr>在一张桌子上
- 如何从三张图片中选择一张?其余的必须是看不见的
- 通过onclick事件将一张图片更改为6张图片
- Sequelize hasMany浏览另一张表
- jm按下停止步骤进入滚动的下一张幻灯片
- 我用JavaScript FIleReader读了一张图片,但是我无法得到图片的信息
- 当 Flickity 到达下一张或上一张幻灯片时的事件
- 单击从一张图像切换到两张图像,然后再切换回一张图像
- 单击另一张图像,显示一张图像几秒钟
- 为什么bxSlider会打断我在最后一张幻灯片和第一张幻灯片之间的转换
- 用d3和topojson绘制一张地图
- 引导程序3转盘-随机选择下一张幻灯片
- 每张图片都比前一张少
- HTML5画布如何在另一张图片上绘制一张图片
- 拍摄一张巨大的网页截图(大小超过1350x13500px)
- 如何在最后一张幻灯片上停止动画
- 完整的背景图片幻灯片jquery,想让幻灯片的最后一张图片可以点击
- jqplot只在一张图表上显示荧光笔
- Javascript-获取一张用户的随机个人资料图片'的朋友在脸书上