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

javascript - Is it possible to get the min and max value with jQuery and render it in another input-field? - Stack Overflow

programmeradmin5浏览0评论

and there we go:

I'm writing(in fact it feels more like trying and asking) a GreaseMonkey script using JS and including(also using) the jQuery library.

I created another table with some input-fields. Example(input-fields-only):


  + "   < input type=\" text\"  id=\" field_1\"  name=\" min_field_1\" value=\"\" > " 
  + "   < input type=\" text\"  id=\" field_2\"  name=\" min_field_2\" value=\"\" > " 
  + "   < input type=\" text\"  id=\" field_3\"  name=\" min_field_3\" value=\"\" > " 
  + "   < input type=\" text\"  id=\" field_4\"  name=\" min_field_4\" value=\"\" > "

//the results should be rendered in another input-field in this case with id=field_result_min + " < input type=\" text\" id=\" field_result_min\" name=\"min_field_result\" > " + " < input type=\" text\" id=\" field_result_max\" name=\"max_field_result\" > " + " < input type=\" text\" id=\" field_result_avg\" name=\"avg_field_result\" > "

My aim was to get the min,max and avg value of the fields and render the result in another input field.

//returns the min value of all input-fields starting with a name attribute of min
$("input[name^='min']")min.()

This won't work because jQuery library hasn't any min() or max() like JS does.

So i tried sth. like


<input type="button" value="=" onclick="window.document.getElementById('field_result_min') = Math.min(window.document.getElementById('field_1').value,window.document.getElementById('field_2').value,window.document.getElementById('field_3').value,window.document.getElementById('field_4').value)" > 

Hm...again I'm stuck and confused - even while I'm explaining.

Question: How do I render the min and max values of some input-fields in one input-field?

Any help would be much appreciated. Faili

Update This is how I solved it. Well, it's just the avg and max(min is deriveable). Any changes or mistakes should e from copy and paste and changing sth. in the code here at stackoverflow.

function calculateAvg() {

var fields = [1*window.document.getElementById('field_1').value,1*window.document.getElementById('field2').value,1*window.document.getElementById('field').value,1*window.document.getElementById('field4').value,1*window.document.getElementById('field5').value,1*window.document.getElementById('field6').value];

var total = 0; var anzahl = 0; for(i=0;i 0) { total = total+fields[i]; anzahl=anzahl+1;} } var durchschnitt = (total/anzahl); window.document.getElementById('fieldavg').value = durchschnitt; } $("#fieldavg").blur (calculateAvg);

function calculateMax() {

var fields = [1*window.document.getElementById('field_1').value,1*window.document.getElementById('field2').value,1*window.document.getElementById('field').value,1*window.document.getElementById('field4').value,1*window.document.getElementById('field5').value,1*window.document.getElementById('field6').value];

var max = 0; for(i=0; i<fields.length;i++) { if(fields[i] > 0) { if(max<fields[i]){ max=fields[i]; } } } window.document.getElementById('field8').value = max;

}

$("'field_8").blur (calculateMax);

So, thanks for your help. I really needed it. Faili

and there we go:

I'm writing(in fact it feels more like trying and asking) a GreaseMonkey script using JS and including(also using) the jQuery library.

I created another table with some input-fields. Example(input-fields-only):


  + "   < input type=\" text\"  id=\" field_1\"  name=\" min_field_1\" value=\"\" > " 
  + "   < input type=\" text\"  id=\" field_2\"  name=\" min_field_2\" value=\"\" > " 
  + "   < input type=\" text\"  id=\" field_3\"  name=\" min_field_3\" value=\"\" > " 
  + "   < input type=\" text\"  id=\" field_4\"  name=\" min_field_4\" value=\"\" > "

//the results should be rendered in another input-field in this case with id=field_result_min + " < input type=\" text\" id=\" field_result_min\" name=\"min_field_result\" > " + " < input type=\" text\" id=\" field_result_max\" name=\"max_field_result\" > " + " < input type=\" text\" id=\" field_result_avg\" name=\"avg_field_result\" > "

My aim was to get the min,max and avg value of the fields and render the result in another input field.

//returns the min value of all input-fields starting with a name attribute of min
$("input[name^='min']")min.()

This won't work because jQuery library hasn't any min() or max() like JS does.

So i tried sth. like


<input type="button" value="=" onclick="window.document.getElementById('field_result_min') = Math.min(window.document.getElementById('field_1').value,window.document.getElementById('field_2').value,window.document.getElementById('field_3').value,window.document.getElementById('field_4').value)" > 

Hm...again I'm stuck and confused - even while I'm explaining.

Question: How do I render the min and max values of some input-fields in one input-field?

Any help would be much appreciated. Faili

Update This is how I solved it. Well, it's just the avg and max(min is deriveable). Any changes or mistakes should e from copy and paste and changing sth. in the code here at stackoverflow.

function calculateAvg() {

var fields = [1*window.document.getElementById('field_1').value,1*window.document.getElementById('field2').value,1*window.document.getElementById('field').value,1*window.document.getElementById('field4').value,1*window.document.getElementById('field5').value,1*window.document.getElementById('field6').value];

var total = 0; var anzahl = 0; for(i=0;i 0) { total = total+fields[i]; anzahl=anzahl+1;} } var durchschnitt = (total/anzahl); window.document.getElementById('fieldavg').value = durchschnitt; } $("#fieldavg").blur (calculateAvg);

function calculateMax() {

var fields = [1*window.document.getElementById('field_1').value,1*window.document.getElementById('field2').value,1*window.document.getElementById('field').value,1*window.document.getElementById('field4').value,1*window.document.getElementById('field5').value,1*window.document.getElementById('field6').value];

var max = 0; for(i=0; i<fields.length;i++) { if(fields[i] > 0) { if(max<fields[i]){ max=fields[i]; } } } window.document.getElementById('field8').value = max;

}

$("'field_8").blur (calculateMax);

So, thanks for your help. I really needed it. Faili

Share Improve this question edited Jul 15, 2010 at 14:03 Faili asked Jul 13, 2010 at 9:16 FailiFaili 1,0005 gold badges14 silver badges27 bronze badges 2
  • Wow, thank you for the responses. I'll get back to you asap and inform you which one worked out for me. – Faili Commented Jul 13, 2010 at 10:07
  • Thanks to all helping me finding a solution. You're just great! – Faili Commented Jul 15, 2010 at 13:52
Add a ment  | 

4 Answers 4

Reset to default 6

Using other libraries are one way to do it, but solving small issues by hand [using raw javascript] would be nicer. Change the selectors in code below, and check.

$(function(){
    sel = $('.field')
    min_field = $('#min');
    max_field = $('#max');
    avg_field = $('#avg');
    len = $(sel).length;
    $(sel).change(function(){
       sum = 0
       list = []
       $(sel).each(function(){
            val = Number($(this).val());
            list.push(val);
            sum += val;
        })
        $(min_field).val(Math.min.apply(Math, list))
        $(max_field).val(Math.max.apply(Math, list))
        $(avg_field).val(sum/len);
     })
})

Happy Coding.

Try this:

var values = $("input[name^='min']").map(function(){
    return isNaN(this.value) ? [] : +this.value;
}).get();

var min = Math.min.apply(null, values);

And then you can add that min-value to another input field like so:

$('#some-input').val(min);

You might want to have a look at the jQuery Calculation Plug-in. The syntax for your example would be something like this:

var MinVal=$("input[name^='min_field_']").min(); // Get minimum value
var MaxVal=$("input[name^='min_field_']").max(); // Get maximum value
var AvgVal=$("input[name^='min_field_']").avg(); // Get average value

This would be faster version:

var MinField=$("input[name^='min_field_']");

var MinVal=MinField.min(); // Get minimum value
var MaxVal=MinField.max(); // Get maximum value
var AvgVal=MinField.avg(); // Get average value

Without plugins you can do something like this JSFiddle

Array.prototype.avg = function() {
    var total = this[0];
    var len = this.length;
    for (var i = 1; i != len; i++) total += this[i];
    return total / len;
}

Array.prototype.min = function() {
    return Math.min.apply( Math, this );
}

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

var values = $("input[id^='field_']").map(function(){
    var value = $(this).val();
    if (value.length == 0 || isNaN(value)) {
        return 0;
    } else {
        return parseFloat(value);
    }
}).get();

$('#max').val(values.max());
$('#min').val(values.min());
$('#avg').val(values.avg());

​ The prototypes should help in future.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论