在Javascript中交换两个值

时间:2015-11-26 17:42:56

标签: javascript

你能告诉我这里有什么问题吗? 我需要交换两个输入元素。输入1进入输入2



document.getElementById('sub').onclick = function(){
  
    var data1 = document.getElementById('in1').value;
    var data2 = document.getElementById('in2').value;
    
     document.getElementById("in1").vlue = data2;
     document.getElementById("in2").vlue = data1;
};

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
</head>
<body>

<form target="">
    <input name="in11" id="in1" value="input #1">
    <input name="in22" id="in2" value="input 2">
</form>
        <button class="swap" id="sub">Swap</a>


<scrpit src="js/javascript.js"></scrpit>
</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

你有一个拼写错误,输入值而不是vlue

document.getElementById('sub').onclick = function(){
  
    var data1 = document.getElementById('in1').value;
    var data2 = document.getElementById('in2').value;
    
     document.getElementById("in1").value = data2;
     document.getElementById("in2").value = data1;
};
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
</head>
<body>

<form target="">
    <input name="in11" id="in1" value="input #1">
    <input name="in22" id="in2" value="input 2">
</form>
        <button class="swap" id="sub">Swap</a>


<scrpit src="js/javascript.js"></scrpit>
</body>
</html>

答案 1 :(得分:1)

你有一个错字。 vlue必须为value

document.getElementById('sub').onclick = function(){
  
    var data1 = document.getElementById('in1').value;
    var data2 = document.getElementById('in2').value;
    
     document.getElementById("in1").value = data2;
     document.getElementById("in2").value = data1;
};
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
</head>
<body>

<form target="">
    <input name="in11" id="in1" value="input #1">
    <input name="in22" id="in2" value="input 2">
</form>
        <button class="swap" id="sub">Swap</a>


<scrpit src="js/javascript.js"></scrpit>
</body>
</html>