Javascript文件,如果偶数位置具有奇数,则所有数字应显示在txt2中,如果奇数位数具有偶数,则所有数字应显示在txt3中,从用户txt1输入数组 在循环中,i + 2不能正常工作,一次显示一个数字,但文本框中不是所有的偶数和奇数:
警告:除非您愿意让浏览器崩溃,否则请勿复制粘贴并尝试运行以下代码!
<head>
<title></title>
</head>
<body>
<table>
<tr>
<td>
Enter data
</td>
<td>
<input type="text" id="txt1" />
</td>
<td>
<input type="text" id="txt2" />
</td>
<td>
<input type="text" id="txt3" />
</td>
</tr>
<tr>
<td>
<input type="button" id="b1" value="REsult" onclick="validate()"/>
</td>
</tr>
</table>
<script>
function validate()
{
var a = document.getElementById("txt1").value.split(",");
for(var i=0;i<a.length;i+2)
{
if(a[i]%2!=0)
{
document.getElementById("txt2").value = a[i];
}
else
{
document.getElementById("txt2").value = " ";
}
}
for(var j=1;j<=a.length;j+2)
{
if(a[j]%2==0)
{
document.getElementById("txt3").value = a[j];
}
else
{
document.getElementById("txt3").value = " ";
}
}
}
</script>
</body>
</html>
design page ] 1
答案 0 :(得分:0)
试试这个:
<!DOCTYPE html>
<html>
<head>
<title>d</title>
<style></style>
<meta charset="UTF-8">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
</head>
<body>
<script>
function validate() {
var a = document.getElementById("txt1").value.split(",");
document.getElementById("txt2").value = '';
document.getElementById("txt3").value = '';
for (var i = 0; i < a.length; i += 2) {
if (a[i] % 2 != 0) {
document.getElementById("txt2").value += a[i] + ',';
}
}
for (var j = 1; j <= a.length; j += 2) {
if (a[j] % 2 == 0) {
document.getElementById("txt3").value += a[j] + ',';
}
}
document.getElementById("txt2").value = document.getElementById("txt2").value.replace(/,$/, '');
document.getElementById("txt3").value = document.getElementById("txt3").value.replace(/,$/, '');
}
</script>
<table>
<tr>
<td>
Enter data
</td>
<td>
<input type="text" id="txt1" />
</td>
<td>
<input type="text" id="txt2" />
</td>
<td>
<input type="text" id="txt3" />
</td>
</tr>
<tr>
<td>
<input type="button" id="b1" value="REsult" onclick="validate()" />
</td>
</tr>
</table>
</body>
</html>
&#13;