jquery如何生成一个While,该While获取所有以String开头的元素并获取最后2个整数
jquery how to make a While that get all element start with String and get last 2 integer
朋友们好,我有一个问题,我想帮忙解决。我有一个不确定数量的元素,格式如下:
<input type="text" id="p_fase_1" name="p_fase_1" value="1" required>
<input type="text" id="p_fase_2" name="p_fase_2" value="1" required>
每个元素的区别在于ID和名称,并以不同的数字结尾。
我需要获取id中以这个id String开头的所有元素的最高数量及其值。
p_fase_"this number" value="this value"
代码:
[id^="p_fase_"]
比如花一些时间处理这些元素,并将最大值保存在变量中
这是我的jsFiddle:https://jsfiddle.net/nuaoch70/
我是这样做的:
$(document).ready(function() {
var highestNumber = 0;
$("input[id^='p_fase_']").each(function() {
var number = $(this).attr("id").replace("p_fase_", "");
highestNumber = Math.max(highestNumber, number);
});
console.log("Highest Number is: " + highestNumber);
});
我需要获得以开头的所有元素中的最高数量该id id中的字符串及其值
因此:
var maxO = $('[id^="p_fase_"]').map(function(){
return {idNum: this.id.split('_').pop(), value: this.value};
}).get().sort(function(a,b){
return a.idNum - b.idNum;
}).pop();
console.log(maxO);
-jsFiddle-
您可以使用映射元素并获得最大值
var max = Math.max.apply(null, $('[id^="p_fase_"]').map(function(){
return this.value;
}));
如果要在name
属性中设置最大数量:
var max = Math.max.apply(null, $('[id^="p_fase_"]').map(function(){
return this.name.split('_').pop();
}));
在id
:中
var max = Math.max.apply(null, $('[id^="p_fase_"]').map(function(){
return this.id.split('_').pop();
}));
相关文章:
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- jquery试图按名称获取按钮位置
- 如何在jQuery中获取元素的形式
- 如何在php文件中获取$.post-ajax传递的值
- 在Shopify中获取博客文章的图片
- 使用Javascript获取所选选项ID
- 在jQuery中获取表的行索引
- 使用jquery将mysql数据获取到新的表行中
- 在动态创建的元素上获取对特定选择器的引用
- 从城市名称获取惊喜
- Angular只从数组中获取所需的数据
- 使用while循环在printArray函数中获取一个额外的未定义值
- jquery如何生成一个While,该While获取所有以String开头的元素并获取最后2个整数
- 获取while循环的条件以打印出一次false
- 如何从while循环中生成/获取数组
- 从while循环中获取变量
- 多维数组-用于获取记录信息的javascript while循环
- JavaScript while循环从其他选择框中获取选择选项和隐藏选项
- 在javaScript中,在while循环期间如何使程序停止以获取用户输入?
- 如何告诉JavaScript函数从PHP while循环中获取哪个id