js中在多个文本框中输入数字然后点击按钮进行排序,排序好的数显示在各自的文本框中
时间: 2023-05-20 19:05:16 浏览: 46
可以使用数组来存储输入的数字,然后使用sort()方法进行排序,最后将排序好的数字分别显示在各自的文本框中。以下是示例代码:
HTML:
```
<input type="text" id="num1">
<input type="text" id="num2">
<input type="text" id="num3">
<button onclick="sortNum()">排序</button>
```
JavaScript:
```
function sortNum() {
var nums = [];
nums.push(parseInt(document.getElementById("num1").value));
nums.push(parseInt(document.getElementById("num2").value));
nums.push(parseInt(document.getElementById("num3").value));
nums.sort(function(a, b){return a-b});
document.getElementById("num1").value = nums[0];
document.getElementById("num2").value = nums[1];
document.getElementById("num3").value = nums[2];
}
```
这段代码将输入的数字存储在数组中,使用sort()方法进行排序,然后将排序好的数字分别显示在各自的文本框中。