У меня есть эта форма:
<input type="text" name="firstTarget" />
<input type="text" name="secondTarget" />
<input type="text" name="thirdTarget" />
<textarea name="result">I have this text and I want to update it using firstTarget. After that I want to use secondTarget and thirdTarget</textarea>
<textarea name="result">I only want to use secondTarget here</textarea>
<textarea name="result">I only want to use thirdTarget here</textarea>
и этот jquery
$('input').keyup(function(){
var regVal=$(this).data('replace');
var newText=$('textarea').val().replace( regVal, this.value);
$(this).data('replace', this.value)
$('textarea').val(newText);
}).each(function(){
$(this).data('replace', this.name);
})
Когда я заполняю вход firstTarget, он заменяет слово "fistTarget" словами, которые записаны на входе firstTarget. Так или иначе, мой код сбрасывает все второй и третий текстовые поля, когда что-то написано на входах. Я не хочу переустанавливать содержимое из textarea 2 или textarea 3. Что я могу сделать?
См. Мой пример здесь: DEMO
var delay = 1000;
var wait = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$('input').keyup(function(){
var regVal=$(this).data('replace');
var that = $(this);
wait(function(){
$('textarea').each(function(){
var newText = $(this).val().replace( regVal, that.val());
$(this).val(newText);
});
}, delay);
}).each(function(){
$(this).data('replace', this.name);
});
Как насчет этого: http://jsfiddle.net/DP4a8/1/?
$('input').keyup(function(){
var input = this;
var regVal=$(this).data('replace');
$('textarea').each(function() {
var newText = this.value.replace(regVal, input.value);
$(this).val(newText);
});
$(this).data('replace', this.value)
}).each(function(){
$(this).data('replace', this.name);
})
Либо я не понимаю вашу цель, либо вы можете быть слишком сложными. Если вы просто пытаетесь связать каждый вход с текстовым полем, это все, что вам нужно:
$('input').keyup(function(){
var target_id = $(this).data('target');
$('#' + target_id).val(this.value);
});
конечно, вам нужно обновить некоторые атрибуты следующим образом:
<input type="text" data-target="firstTarget" />
<input type="text" data-target="secondTarget" />
<input type="text" data-target="thirdTarget" />
<textarea style="width:500px; height: 100px;" id="firstTarget">I have this text and I want to update it using firstTarget. After that I want to use secondTarget and thirdTarget</textarea>
<textarea style="width:500px; height: 100px;" id="secondTarget">I only want to use secondTarget here</textarea>
<textarea style="width:500px; height: 100px;" id="thirdTarget">I only want to use thirdTarget here</textarea>
Вот демо.