Javascript;显示几个变量? [英] Javascript ; Display several variables ?

查看:72
本文介绍了Javascript;显示几个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
<head>
<title>Trouver le plus petit nombre</title>
<script type='text/javascript'>
function Min_nombre(A,B,C,D)
{
	var A=parseFloat(A);
	var B=parseFloat(B);
	var C=parseFloat(C);
	var D=parseFloat(D);
	var res;
	
				   if(A > B && A > C && A > D )
						 res = B,C,D;
				   if(B > A && B > C && B > D)
						 res = A,C,D;
				   if(C > A && C > B && C > D )
						 res = A,B,D;							 
				   else res = A,B,C; //shows only 1st variable :-(				   

						console.log(res);
					
 window.document.getElementById('resultat').value = res;
}

</script>
</head>

<body>
<p>Definition du nombre MIN</p>
<input type='text' name='A' id='A' VALUE="A">
<input type='text' name='B' id='B' VALUE="B">
<input type='text' name='C' id='C' VALUE="C">
<input type='text' name='D' id='D' VALUE="D">
<input type='text' name='Resultat' id='resultat'>
<input type='submit' value='GO' />
</body>
</html>





-此代码正在搜索4中最小的3个数字 -

当我启动此代码 var res 仅显示第一个 VAR A ,其他两个不显示。哪里弄错了?我使用了错误的语法或逻辑?



-This code is searching for 3 smallest number in 4 given-
When i m launching this code the var res is displaying only the first VAR A and the other two doesn t. Where i made mistake ? I used wrong syntax or logic?

推荐答案

简单的解决方案是:

Simple solution would be:
res = "A=" + A + "; B=" + B + "; C=" + C;



连接中的第一个操作数是字符串,因此它会将其他(非字符串)操作数转换为字符串并将它们连接起来。



如需更多通用和高级方法,请参阅以下有用的讨论:

http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format [ ^ ],

http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery [ ^ ]。



-SA


我猜你想把三个变量A,B和C显示为一个字符串。 Javascript不使用','运算符进行连接,你必须使用'+'运算符。



由于值是数字,你不能只使用'+'本身用来创建一个字符串,因为这会将值加在一起 ​​- 你必须连接它们之间的字符串:

I'm guessing you want to display the three variables A, B and C as a string. Javascript doesn't use the ',' operator for concatenation, you must use the '+' operator.

Since the values are numeric, you can't just use '+' by itself to make a string, since that would add the values together - you have to concatenate with strings in between:
res = A + ' ' + B + ' ' + C;





或者你可以把它们放在一个数组中并加入它们:



or you can put them in an array and join them:

res = [A, B, C].join(' ');





或者您可以将数组直接转储到控制台,而无需将其作为字符串加入:



or you can dump the array directly to the console without joining it as a string:

res = [A, B, C];
console.log(res);


如果您去调试代码你会看到 res = A,B,C 部分只分配 A 并遗漏 B C ...这是JavaScript的小巧之一 - 随心所欲,不设置任何错误...

我假设您想要的是以逗号分隔的值列表...

首先 - 使用正确的语法创建一个数组:

If you go and debug your code you will see that the res = A, B, C part assigned only A and left out B and C...That's one of the small beauties of JavaScript - do as it please and set no errors...
I assume that what you wanted is a comma separated list of the values...
First - create an array with the proper syntax:
else res = [A,B,C];



现在你可以分配它只是之前的输入框的value属性,神奇地你会得到一个逗号分隔的值列表......但没有什么神奇的,它是JavaScript将你的数组转换为字符串的结果(字符串是value属性的类型) )...

如果你想要对话的力量(例如你想设置一个永远不会初始化的新变量)你可以这样做:


Now you can assign it to the value property of the input box just before and magically you will got a comma separated list of values...But nothing magical about it, it's the result of JavaScript converting your array to string (string is the type of the value property)...
If you want the force that conversation (eg you want to set a new variable never initialized) you can do it like this:

var newVar = "" + res;



前面的字符串(事件为空字符串)将强制JavaScript转换res(这是一个数组) )转换为字符串,转换将导致逗号分隔列表...


The preceding string (event an empty string) will force JavaScript to convert res (which is an array) to string and that convert will result in a comma separated list...


这篇关于Javascript;显示几个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆