Fai*_*ili 1 javascript jquery greasemonkey
然后我们去:
我正在编写(实际上感觉更像是尝试和询问)使用JS的GreaseMonkey脚本并包括(也使用)jQuery库.
我用一些输入字段创建了另一个表.实施例(输入字段只):
+ " < 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\" > "
我的目标是获取字段的最小值,最大值和平均值,并将结果呈现在另一个输入字段中.
//returns the min value of all input-fields starting with a name attribute of min
$("input[name^='min']")min.()
这不起作用,因为jQuery库没有像JS那样的min()或max().
所以我试过了...... 喜欢
<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)" >
嗯...我再次陷入困境和困惑 - 即使在我解释的时候.
问题:如何在一个输入字段中渲染某些输入字段的最小值和最大值?
任何帮助将非常感激.Faili
更新 这是我解决它的方式.好吧,它只是平均值和最大值(最小值是可推导的).任何更改或错误都应来自复制和粘贴以及更改某些内容.在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);
所以,谢谢你的帮助.我真的需要它.Faili
使用其他库是一种方法,但手工解决小问题[ 使用原始javascript ]会更好.在下面的代码中更改选择器,然后检查.
$(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);
})
})
Run Code Online (Sandbox Code Playgroud)
快乐的编码.