是否可以使用 jQuery 获取最小值和最大值并将其呈现在另一个输入字段中?
就这样:
我正在使用 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=\"\" > "
// 在本例中,结果应呈现在另一个输入字段中,id=field_result_min
+ " <输入类型=\"文本\" id=\"field_result_min\" name=\"min_field_result\" > "
+ " <输入类型=\"文本\" id=\"field_result_max\" name=\"max_field_result\" > "
+ " <输入类型=\"文本\" 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)" >
嗯……我再次陷入困境和困惑——即使在我解释的时候。
问题:如何在一个输入字段中呈现某些输入字段的最小值和最大值?
任何帮助将不胜感激。 菲力
更新 我就是这样解决的。好吧,这只是平均值和最大值(最小值是可导出的)。任何更改或错误都应该来自复制、粘贴和更改某物。在 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];
总变量 = 0;
var 安扎尔 = 0;
for(i=0;i 0) {
总计=总计+字段[i];
安扎尔=安扎尔+1;}
}
var durchschnitt =(总计/anzahl);
window.document.getElementById('fieldavg').value = durchschnitt;
}
$("#fieldavg").blur (calculateAvg);
函数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];
最大变量=0;
for(i=0; i 0){
if(max<字段[i]){
最大值=字段[i];
}
}
}
window.document.getElementById('field8').value = max;
}
$("'field_8").blur (calculateMax);
所以,感谢您的帮助。我真的需要它。 法力
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 come 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用其他库是一种方法,但手动解决小问题[使用原始 javascript] 会更好。
更改下面代码中的选择器,然后检查。
快乐编码。
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.
Happy Coding.
试试这个:
然后您可以将该最小值添加到另一个输入字段,如下所示:
Try this:
And then you can add that min-value to another input field like so:
您可能想看看 jQuery 计算插件。您的示例的语法如下所示:
这将是更快的版本:
You might want to have a look at the jQuery Calculation Plug-in. The syntax for your example would be something like this:
This would be faster version:
如果没有插件,您可以执行类似 this JSFiddle 的操作
原型应该会在未来有所帮助。
Without plugins you can do something like this JSFiddle
The prototypes should help in future.