简体   繁体   English

jQuery .click切换文本输入框

[英]Jquery .click Switch Text Input boxes

I've been trying all day to get the below code to work. 我一直在努力使以下代码正常工作。 Basically what I want it to do is swap the text between the input boxes when the link is clicked. 基本上,我希望它执行的操作是单击链接时在输入框之间交换文本。 So the text from A to B and B to A. Any idea on how I can fix this or what I'm doing wrong? 因此,从A到B,从B到A的文本。关于如何解决此问题或我做错了的任何想法?
I've tried the below: 我尝试了以下方法:

 <script>
 $("#switchdir").click(function () 
            {
    $("#saddr").val($('#daddr').val());
    $("#daddr").val($('#saddr').val());
            }

 </script>


<form action="directions1.php" method="get">
  <h1><p>A: <input type="text"  class="directionsdata"  name="saddr"       placeholder="Enter Address, business or landmark" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Address, business or landmark'" style="color: CCCCCC; width:290px; height: 20px; padding:5px; border-radius: 5px;  border-style: solid; border-color: #CCCCCC;" /></p></h1> 
  <h1><p>B: <input type="text" class="directionsdata" name="daddr"  placeholder="Enter Address, business or landmark" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Address, business or landmark'" style="width:290px; height: 20px;padding:5px; border-radius: 5px;  border-style: solid; border-color: #CCCCCC;"/></p></h1> 
  <a href="#" id="switchdir">Switch</a>
  <input type="submit" value="Get Directions" class="button"/>
  </form>

Before assigning the value to the first input field you need to store the value in a variable, else once you assign the value you will loose it. 在将值分配给第一个输入字段之前,您需要将值存储在变量中,否则,一旦分配了值,您将失去它。 Also your id selector is wrong as there is not id attribute for the input elements, use name attribute along with attribute selector instead 另外,您的id选择器是错误的,因为输入元素没有id属性,请改用name属性和属性选择器

jQuery(function () {
    $("#switchdir").click(function (e) {
        var daddr = $('input[name="daddr"]').val();
        $('input[name="daddr"]').val($('input[name="saddr"]').val());
        $('input[name="saddr"]').val(daddr);
    })
})

Demo: Fiddle 演示: 小提琴

You need a temporary variable. 您需要一个临时变量。 Also, you had a missing closed ) ... or maybe that was just a copy/paste error. 此外,您还缺少一个封闭的)...或可能只是复制/粘贴错误。 Either way I fixed it. 无论哪种方式,我都将其修复。

Try this: 尝试这个:

$("#switchdir").click(function () {
    var saddr=$("#saddr").val();
    $("#saddr").val($('#daddr').val());
    $("#daddr").val(saddr);
});

Here's an example, but you do need to first save one of the values that you'll start swapping with. 这是一个示例,但是您确实需要首先保存将要与之交换的值之一。

http://jsfiddle.net/mXna3/ http://jsfiddle.net/mXna3/

html: 的HTML:

<input id="alpha" type="text"><br/>
<input id="beta" type="text">

<div id="swapper">Click me to swap input texts</div>

JS: JS:

$('#swapper').on('click', function() {
    var alpha = $('#alpha'),
        beta = $('#beta'),
        temp = null;
    // Cache the first input's value
    temp = alpha.val();
    // Place 2nd inputs value in first
    alpha.val(beta.val());
    // Place first input value in second's
    beta.val(temp);
});

The answers above are correct, my additions include that you give values to the id attributes of the input elements as well. 上面的答案是正确的,我的补充内容是您还给输入元素的id属性赋予了值。 It makes your js code cleaner and it is easier to access using jquery. 它使您的js代码更整洁,并且更易于使用jquery进行访问。

<input type="text"  id="a_input" class="directionsdata"  name="saddr" ....

and

<input type="text" id="b_input" class="directionsdata" name="daddr" ....

JavaScript code below 下面的JavaScript代码

$("#switchdir").click(function () {
    var a_val = $("#a_input").val();
    var b_val = $("#b_input").val();
    $("#a_input").val(b_val);
    $("#b_input").val(a_val);
});

Demo: Fiddle 演示: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM