[英]How to switch text from two different inputs on button click?
I am trying to take the input of two different text boxes, and swap the text on a button click 我正在尝试接受两个不同文本框的输入,并在单击按钮时交换文本
Sadly, nothing is happening. 可悲的是,什么都没有发生。 Am I missing something super simple?
我错过了一些超级简单的东西吗?
<input type="text" id="string1" class="str_1 inputs" value="type something pointless"/>
<input type="button" onClick="switchITup();" id="theButton" value="do something amazing">
<input type="text" id="string2" class="str_2 inputs" value="type something meaningful"/>
function switchITup(){
var val1 = $('#string1').val;
var val2 = $('#string2').val;
$('#string1').text(val2);
$('#string2').text(val1);
$('#string1').removeClass('inputs');
$('#string1').addClass('YooHoo');
};
In your code ( val
need to be val()
and text()
need to be val()
):- 在您的代码中(
val
需要为val()
和text()
需要为val()
):
function switchITup(){ var val1 = $('#string1').val(); var val2 = $('#string2').val(); $('#string1').val(val2); $('#string2').val(val1); $('#string1').removeClass('inputs'); $('#string1').addClass('YooHoo'); };
.inputs{ font-size:20px; } .YooHoo{ font-size:12px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="string1" class="str_1 inputs" value="type something pointless"/> <input type="button" onClick="switchITup();" id="theButton" value="do something amazing"> <input type="text" id="string2" class="str_2 inputs" value="type something meaningful"/>
Note:- added some style to show you that it worked fine 注意:-添加了一些样式以向您展示它工作正常
我相信应该是.val()
而不是.val
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("input:text").val("new value"); }); }); </script> </head> <body> <p>Name: <input type="text" name="user"></p> <button>Set the value of the input field</button> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.