Javascript 从两个输入中找出较大的数字
我正试图写一个小程序,只选择较大的数字。我的问题是,它希望我在.js文件中使用if-else语句。这是我的htmlJavascript 从两个输入中找出较大的数字,javascript,html,Javascript,Html,我正试图写一个小程序,只选择较大的数字。我的问题是,它希望我在.js文件中使用if-else语句。这是我的html <body> BOX 1<input id = 'box1' name = '' value = '' class = ''><br> BOX 2<input id = 'box2' name = '' value = '' class = ''><br> BIGGER<input id =
<body>
BOX 1<input id = 'box1' name = '' value = '' class = ''><br>
BOX 2<input id = 'box2' name = '' value = '' class = ''><br>
BIGGER<input id = 'bigger' name = '' value = '' class = ''><br>
<button id = 'go' class = ''>GO</button>
<script src = 'js/javascript 03.js'></script>
</body>
首先,我建议您避免在属性和参数之间使用空格。虽然它似乎工作起来都一样,但我从未见过这样的HTML代码 这就是我想到的:
<html>
<body>
BOX 1 <input id='box1' name='' value='' class=''><br>
BOX 2 <input id='box2' name='' value='' class=''><br>
BIGGER<input id='bigger' name='' value='' class=''><br>
<button id='go' class=''> GO</button>
<script type="text/javascript">
document.getElementById('go').onclick = function() {
// Let's get the values and convert them to integers
var val1 = parseInt(document.getElementById('box1').value);
var val2 = parseInt(document.getElementById('box2').value);
// Let's pick the bigger one and put it in another variable, "bigger"
if (val1 > val2)
var bigger = val1;
else
var bigger = val2;
// Let's write the value we just picked to the field whose id is "bigger"
document.getElementById('bigger').value = bigger;
}
</script>
</body>
</html>
js文件的代码在哪里?我把它添加到原始的postnumber1=parseFloatnumber1?你是说number1=parseFloatbox1吗?是的,我想这会是个问题。你可以用biger=而不是if/else来找到更大的数字。没问题!快乐编码:
<html>
<body>
BOX 1 <input id='box1' name='' value='' class=''><br>
BOX 2 <input id='box2' name='' value='' class=''><br>
BIGGER<input id='bigger' name='' value='' class=''><br>
<button id='go' class=''> GO</button>
<script type="text/javascript">
document.getElementById('go').onclick = function() {
// Let's get the values and convert them to integers
var val1 = parseInt(document.getElementById('box1').value);
var val2 = parseInt(document.getElementById('box2').value);
// Let's pick the bigger one and put it in another variable, "bigger"
if (val1 > val2)
var bigger = val1;
else
var bigger = val2;
// Let's write the value we just picked to the field whose id is "bigger"
document.getElementById('bigger').value = bigger;
}
</script>
</body>
</html>