如何同步两个文本框的表单值?

时间:2021-01-26 15:41:51

Hi all i am new to jQuery. Suppose I have two HTML text boxes. How can I make it happen that if I write in text box A then same value goes to textbox B and if I write in B then it goes to A and same as delete the text. How can this be done in jQuery?

大家好我是jQuery的新手。假设我有两个HTML文本框。如果我在文本框A中写入,那么相同的值将转到文本框B,如果我在B中写入,那么它将转到A,与删除文本相同。怎么能在jQuery中完成?

6 个解决方案

#1


40  

This is a more dynamic way:

这是一种更动态的方式:

<input class="copyMe" type="text" />
<input class="copyMe" type="text" />
<input class="copyMe" type="text" />
<input class="copyMe" type="text" />

And the code:

和代码:

$(document).ready(function(){
    $(".copyMe").keyup(function(){
        $(".copyMe").val($(this).val());
    });
});

Prove of concept: http://jsfiddle.net/MQXCt/

概念证明:http://jsfiddle.net/MQXCt/

#2


5  

This is quite easy:

这很简单:

$("#textBoxA").keyup(function(){
   $("#textBoxB").val($("#textBoxA").val());
});

$("#textBoxB").keyup(function(){
   $("#textBoxA").val($("#textBoxB").val());
});

#3


3  

A slightly more efficient way than the most upvoted answer is:

比最热烈的答案更有效的方式是:

var $inputs = $(".copyMe");  
$inputs.keyup(function () {
     $inputs.val($(this).val());
});

http://css-tricks.com/snippets/jquery/keep-text-inputs-in-sync/

http://css-tricks.com/snippets/jquery/keep-text-inputs-in-sync/

#4


1  

You'll have to put a function call on each textbox's onchange events, that will take the value of one box and put it in the other.

你必须在每个文本框的onchange事件上放置一个函数调用,它将获取一个框的值并将其放在另一个框中。

#5


1  

I ran into this challenge today. I made a small plugin to make the code more readable and to handle text inputs, but also select fe.

我今天遇到了这个挑战。我制作了一个小插件,使代码更具可读性并处理文本输入,但也选择了fe。

When you include the plugin, you can simply use $("selector").keepInSync($("otherselector"));

当你包含插件时,你可以简单地使用$(“selector”)。keepInSync($(“otherselector”));

$.fn.keepInSync = function($targets) {
  // join together all the elements you want to keep in sync
  var $els = $targets.add(this);
  $els.on("keyup change", function() {
    var $this = $(this);
    // exclude the current element since it already has the value
    $els.not($this).val($this.val());
  });
  return this;
};

//use it like this
$("#month1").keepInSync($("#month2, #month3"));
$("#text1").keepInSync($("#text2"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Two way sync</h1>
<select id="month1">
  <option value="">Please select a moth</option>
  <option value="jan">January</option>
  <option value="feb">February</option>
  <option value="mar">March</option>
</select>
<select id="month2">
  <option value="">Please select a moth</option>
  <option value="jan">1</option>
  <option value="feb">2</option>
  <option value="mar">3</option>
</select>
<select id="month3">
  <option value="">Please select a moth</option>
  <option value="jan">Jan</option>
  <option value="feb">Feb</option>
  <option value="mar">Mar</option>
</select>
<br>
<input type="text" id="text1">
<input type="text" id="text2">

#6


0  

Using Snicksie's method, you can see the live demo below. I have included a button to reset the textbox values as well.

使用Snicksie的方法,您可以在下面看到现场演示。我还包含一个按钮来重置文本框值。

http://jsfiddle.net/refhat/qX6qS/5/

http://jsfiddle.net/refhat/qX6qS/5/

#1


40  

This is a more dynamic way:

这是一种更动态的方式:

<input class="copyMe" type="text" />
<input class="copyMe" type="text" />
<input class="copyMe" type="text" />
<input class="copyMe" type="text" />

And the code:

和代码:

$(document).ready(function(){
    $(".copyMe").keyup(function(){
        $(".copyMe").val($(this).val());
    });
});

Prove of concept: http://jsfiddle.net/MQXCt/

概念证明:http://jsfiddle.net/MQXCt/

#2


5  

This is quite easy:

这很简单:

$("#textBoxA").keyup(function(){
   $("#textBoxB").val($("#textBoxA").val());
});

$("#textBoxB").keyup(function(){
   $("#textBoxA").val($("#textBoxB").val());
});

#3


3  

A slightly more efficient way than the most upvoted answer is:

比最热烈的答案更有效的方式是:

var $inputs = $(".copyMe");  
$inputs.keyup(function () {
     $inputs.val($(this).val());
});

http://css-tricks.com/snippets/jquery/keep-text-inputs-in-sync/

http://css-tricks.com/snippets/jquery/keep-text-inputs-in-sync/

#4


1  

You'll have to put a function call on each textbox's onchange events, that will take the value of one box and put it in the other.

你必须在每个文本框的onchange事件上放置一个函数调用,它将获取一个框的值并将其放在另一个框中。

#5


1  

I ran into this challenge today. I made a small plugin to make the code more readable and to handle text inputs, but also select fe.

我今天遇到了这个挑战。我制作了一个小插件,使代码更具可读性并处理文本输入,但也选择了fe。

When you include the plugin, you can simply use $("selector").keepInSync($("otherselector"));

当你包含插件时,你可以简单地使用$(“selector”)。keepInSync($(“otherselector”));

$.fn.keepInSync = function($targets) {
  // join together all the elements you want to keep in sync
  var $els = $targets.add(this);
  $els.on("keyup change", function() {
    var $this = $(this);
    // exclude the current element since it already has the value
    $els.not($this).val($this.val());
  });
  return this;
};

//use it like this
$("#month1").keepInSync($("#month2, #month3"));
$("#text1").keepInSync($("#text2"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Two way sync</h1>
<select id="month1">
  <option value="">Please select a moth</option>
  <option value="jan">January</option>
  <option value="feb">February</option>
  <option value="mar">March</option>
</select>
<select id="month2">
  <option value="">Please select a moth</option>
  <option value="jan">1</option>
  <option value="feb">2</option>
  <option value="mar">3</option>
</select>
<select id="month3">
  <option value="">Please select a moth</option>
  <option value="jan">Jan</option>
  <option value="feb">Feb</option>
  <option value="mar">Mar</option>
</select>
<br>
<input type="text" id="text1">
<input type="text" id="text2">

#6


0  

Using Snicksie's method, you can see the live demo below. I have included a button to reset the textbox values as well.

使用Snicksie的方法,您可以在下面看到现场演示。我还包含一个按钮来重置文本框值。

http://jsfiddle.net/refhat/qX6qS/5/

http://jsfiddle.net/refhat/qX6qS/5/