jquery 如何制作一个让所有元素都以 String 开头并获得最后 2 个整数的 While
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 字符串开头的所有元素的最大数量及其所具有的值。
p_fase_"this number" value="this value"
代码:
[id^="p_fase_"]
喜欢花点时间处理这些元素并将最大值保存在变量中
I require to get the highest number of all element that start with
this id String in the ID and the value it has
所以会是:
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);
您可以使用以下方法映射元素并获取最大值:
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();
}));
这是我的 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);
});
朋友你好我有问题,想帮忙解决。 我有以下格式的不确定数量的元素:
<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 字符串开头的所有元素的最大数量及其所具有的值。
p_fase_"this number" value="this value"
代码:
[id^="p_fase_"]
喜欢花点时间处理这些元素并将最大值保存在变量中
I require to get the highest number of all element that start with this id String in the ID and the value it has
所以会是:
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);
您可以使用以下方法映射元素并获取最大值:
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();
}));
这是我的 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);
});