使用javascript交换两个文本框中的值

使用javascript交换两个文本框中的值,javascript,variables,Javascript,Variables,我正在制作一个简单的计算器,有4个输入a、b、c和d,需要能够交换c和d的值。例如,文本框c的值为45,文本框d的值为75。单击一个按钮以交换值,因此c=75&d=45如果您使用jQuery,并且您的输入具有正确的ID,则应执行以下操作: var t = $('#c').val(); $('#c').val($('#d').val()); $('#d').val(t); 这很简单,但是…如果您使用jQuery,并且您的输入具有正确的ID,那么这应该可以: var t = $('#c').val

我正在制作一个简单的计算器,有4个输入a、b、c和d,需要能够交换c和d的值。例如,文本框c的值为45,文本框d的值为75。单击一个按钮以交换值,因此c=75&d=45

如果您使用jQuery,并且您的输入具有正确的ID,则应执行以下操作:

var t = $('#c').val();
$('#c').val($('#d').val());
$('#d').val(t);

这很简单,但是…

如果您使用jQuery,并且您的输入具有正确的ID,那么这应该可以:

var t = $('#c').val();
$('#c').val($('#d').val());
$('#d').val(t);

这很简单,但是…

我假设您的HTML看起来像这样:

<input id="input-c">
<input id="input-d">
var temp = $("#input-c").val();
$("#input-c").val($("#input-d").val());
$("#input-d").val(temp);
var inputC = document.getElementById("input-c");
var inputD = document.getElementById("input-d");
var temp = inputC.value;
inputC.value = inputD.value;
inputD.value = temp;
如果需要,您可以对此进行一点优化,但它增加了几行:

var $inputC = $("#input-c");
var $inputD = $("#input-d");
var temp = $inputC.val();
$inputC.val($inputD.val());
$inputD.val(temp);
如果不使用jQuery,可以执行以下操作:

<input id="input-c">
<input id="input-d">
var temp = $("#input-c").val();
$("#input-c").val($("#input-d").val());
$("#input-d").val(temp);
var inputC = document.getElementById("input-c");
var inputD = document.getElementById("input-d");
var temp = inputC.value;
inputC.value = inputD.value;
inputD.value = temp;

通常,在交换两个变量的值时,这是一种常见的编程模式。您必须先创建一个临时变量,然后才能进行交换,否则一个变量将冲击另一个。

我假设您的HTML如下所示:

<input id="input-c">
<input id="input-d">
var temp = $("#input-c").val();
$("#input-c").val($("#input-d").val());
$("#input-d").val(temp);
var inputC = document.getElementById("input-c");
var inputD = document.getElementById("input-d");
var temp = inputC.value;
inputC.value = inputD.value;
inputD.value = temp;
如果需要,您可以对此进行一点优化,但它增加了几行:

var $inputC = $("#input-c");
var $inputD = $("#input-d");
var temp = $inputC.val();
$inputC.val($inputD.val());
$inputD.val(temp);
如果不使用jQuery,可以执行以下操作:

<input id="input-c">
<input id="input-d">
var temp = $("#input-c").val();
$("#input-c").val($("#input-d").val());
$("#input-d").val(temp);
var inputC = document.getElementById("input-c");
var inputD = document.getElementById("input-d");
var temp = inputC.value;
inputC.value = inputD.value;
inputD.value = temp;
通常,在交换两个变量的值时,这是一种常见的编程模式。在进行交换之前,必须先创建一个临时变量,否则一个变量将冲击另一个。

Javascript:

function swapValues(){
var tmp = document.getElementById("c").value;
document.getElementById("c").value = document.getElementById("d").value;
document.getElementById("d").value = tmp;

}
和HTML:

<input type="text"  id="a" value="1st" />
<input type="text"  id="b" value="2nd" />
<input type="text"  id="c" value="3rd" />
<input type="text"  id="d" value="4th" />
<input type="button"  id="go" onclick="swapValues()" value="Swap">

Javascript:

function swapValues(){
var tmp = document.getElementById("c").value;
document.getElementById("c").value = document.getElementById("d").value;
document.getElementById("d").value = tmp;

}
和HTML:

<input type="text"  id="a" value="1st" />
<input type="text"  id="b" value="2nd" />
<input type="text"  id="c" value="3rd" />
<input type="text"  id="d" value="4th" />
<input type="button"  id="go" onclick="swapValues()" value="Swap">

使用香草javascript时,概念可能是这样的

HTML

<input type="text" placeholder="od" class="firstInput" />
<input type="text" placeholder="do" class="secondInput" />
<span class="inputExchange">
    <i class="fas fa-exchange-alt float-right"></i>
</span>

这个概念可以是这样的使用香草javascript

HTML

<input type="text" placeholder="od" class="firstInput" />
<input type="text" placeholder="do" class="secondInput" />
<span class="inputExchange">
    <i class="fas fa-exchange-alt float-right"></i>
</span>

没有理由让jQuery参与其中,所以答案的最后一部分是要走的路。@jaimetores,如果OP已经在他的页面中使用jQuery,那么使用它是有意义的。当然,只为这三行加载jQuery会有点过分。@jcaron我完全同意,但这听起来像是家庭作业,我怀疑这是真的。而且没有放置
jQuery
标记。@JaimeTorres我的第一反应是对“你写了什么代码?”或“实际问题是什么?”的问题发表评论,但后来我变软了…:-)没有理由让jQuery参与其中,所以答案的最后一部分是要走的路。@jaimetores,如果OP已经在他的页面中使用jQuery,那么使用它是有意义的。当然,只为这三行加载jQuery会有点过分。@jcaron我完全同意,但这听起来像是家庭作业,我怀疑这是真的。而且没有放置
jQuery
标记。@JaimeTorres我的第一反应是对“你写了什么代码?”或“实际问题是什么?”的问题发表评论,但后来我变软了…:-)