mixing $(this) value in jquery
How to prevent mixing values in jquery if i use $(this)? For example:
1: <input type="text" id="in1" class="test"> <br />
2: <input type="text" id="in2" class="test"> <br />
<div id="ppp"> <p class="show" id="one">onevalue</p> <p class="show" id="two">twovalue</p><开发者_运维百科;/div>
#ppp {
display: none;
}
$(".click").click(function(e) {
$("#block").show();
var current = $(this);
$(".show").click(function(e) {
current.val($(this).attr("id"));
});
});
live example - http://jsfiddle.net/VmXU9/59/
If i clicked in 1 input show me "onevalue" and "twovalue". If i clicked "onevalue" or "twovalue" that in 1 input appears id of "onevalue" or "twovalue". This is good, but if i additionally clickes 2 input and choose "onevalue" or "twovalue" that ID of "onevalue" or "twovalue" appears in 1 input and 2 intput. Should be only in one - in last clicked. How can i it make?
Thanks!
You're binding the $(".show").click()
every time an input is clicked, causing it to duplicate. Make sure to unbind it first:
$(".show").unbind('click').click(function(e) {
//etc
});
If I understand what you're trying to do, you need to:
- Move the scope of
current
- Move the binding of the click event on
input.show
elements to outside the click ofinput.click
elements:
Demo: http://jsfiddle.net/JAAulde/VmXU9/65/
Ideally, you'd close the scope over all of this to avoid introducing current
as global, and you'd check that current
had been changed from its initial setting before operating on it:
Demo: http://jsfiddle.net/JAAulde/VmXU9/66/
you have to unbind the the click for the .show. add:
$('.show').unbind('click');
as in the example linked to below
live example - http://jsfiddle.net/VmXU9/62/
精彩评论