最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - how to get highest input field value in javascript - Stack Overflow

programmeradmin2浏览0评论

I want to get highest value of this field. How I can do this?

 <input type="text" style="width:20%;" class="input-text" name="position[]" value="20" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="25" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="10" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="5" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="30" />

I want to get highest value of this field. How I can do this?

 <input type="text" style="width:20%;" class="input-text" name="position[]" value="20" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="25" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="10" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="5" />
 <input type="text" style="width:20%;" class="input-text" name="position[]" value="30" />
Share Improve this question edited Jun 9, 2015 at 4:49 scniro 17k8 gold badges66 silver badges107 bronze badges asked Jun 9, 2015 at 4:29 Qaisar SattiQaisar Satti 2,7622 gold badges20 silver badges36 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

Pure javascript:

var inputs = document.querySelectorAll('input[name="position[]"]');
var max =0;
for (var i = 0; i < inputs.length; ++i) {
   max =  Math.max(max , parseInt(inputs[i].value));
}

Others may chime in with a vanilla solution, but if you are using jQuery here is a way you can do so

Array.max = function(array) {
    return Math.max.apply(Math, array);
};

var max = Array.max($('.input-text').map(function() {
    return $(this).val();
}));

console.log(max) // 30

JSFiddle Link

var maxVal = 0;
$('input[name="position[]"]').each(function(){
    maxVal = Math.max(maxVal , parseInt($(this).val()));
});
alert(maxVal);

Try like this

HTML:

<form name="myForm">
  <input type="text" style="width:20%;" class="input-text" name="position[]" value="20" />
  <input type="text" style="width:20%;" class="input-text" name="position[]" value="25" />
  <input type="text" style="width:20%;" class="input-text" name="position[]" value="10" />
  <input type="text" style="width:20%;" class="input-text" name="position[]" value="5" />
  <input type="text" style="width:20%;" class="input-text" name="position[]" value="30" />
</form>

Javascript:

var myForm = document.forms.myForm;
var myControls = myForm.elements['position[]'];
var max = -Infinity;
for (var i = 0; i < myControls.length; i++) {
    if( max<parseInt(myControls[i]))
      max=parseInt(myControls[i]);
}
console.log(max);

Getting highest input value using jQuery each. Demo

var inputValue = -Infinity;
$("input:text").each(function() {
    inputValue = Math.max(inputValue, parseFloat(this.value));
});
alert(inputValue);
发布评论

评论列表(0)

  1. 暂无评论