本文介绍了Textarea问题,当用户键入,但文字不'改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题。我有选择选项。当选择一个选项时,textarea中的文本更改,一切正常,而用户不尝试在文本区域中写入内容,当用户输入时,则文本不再更改。这里是代码:
选择选项:

 < select class =esid =es1> ; 
< option id =edu1> 1< / option>
< option id =edu2> 2< / option>
< / select>

这里是我使用的jQuery代码(在这段代码中,我选择了选项id,然后通过id数字我显示文本,一切正常工作)

  $('#es1')。change(function(){
$('#sk1')。html('');
var id = $(this).children(:selected)。attr(id);
var idd = id.split('edu');
var num = idd [1];
$('#sk1')。html(customTextByNumber [num]);
});

,并且我有id =sk1的文本区域。最有趣的是,当用户在框中输入一些额外的东西时,当我选择其他选项时,文本不会改变,但当我查看检查元素时(在谷歌浏览器中),我可以看到文本正在改变,但是在标签。例如,当用户不输入任何内容时,所有内容都显示在html中,如下所示:

 < textarea id =sk1>文字在这里< / textarea> 

用户输入一些额外内容后:

 < textarea id =sk1> ecustom文字和用户文字< / textarea>未向用户显示的另一个选项文本< / textarea> 

也许你有一些想法如何解决这样的问题?

已解决如果有人遇到此类问题,请使用此

  $('#sk1' ).VAL(东西); 

而不是这一个

  $( '#SK1')的HTML( '某物')。 (这是坏的)


解决方案

您需要使用 .val()方法放在 textarea



<$ p $ ()#$ b $('#sk1')。val(''); //这一行可能是不必要的...

$('#es1')。
var id = $(this).children(:selected)。attr(id);
var idd = id.split('edu');
var num = idd [1];
$('#sk1').val(customTextByNumber [num]);
});

我不知道 customTextByNumber 是什么,但它应该在编辑 textarea


I have a little issue. I have a select with options. When one option is selected, then text changes in textarea, everything is working perfectly while user don't try to write something in text area, when user types, then text doesn't change anymore. Here is code:select with option:

   <select class="es" id="es1">
    <option id="edu1">1</option>
    <option id="edu2">2</option>
</select>

here is jQuery code which I use(in this code I am taking selected option id and then by id number I am showing text, everything is working perfectly)

$('#es1').change(function() {
                    $('#sk1').html('');
                    var id = $(this).children(":selected").attr("id");
                    var idd = id.split('edu');
                    var num = idd[1];
                    $('#sk1').html(customTextByNumber[num]);
                });

and I have text area with id="sk1". The most interesting is that, when user types something extra into box, when I select other option, text doesn't change in it, but when I look at inspect element (in google chrome) I can see that text is changing, but after tag. For example when user don't type everything is showing in html like this:

<textarea id="sk1">text here</textarea>

After user types something extra:

<textarea id="sk1">ecustom text and user text</textarea> Another options text which is not shown to user</textarea>

Maybe you have some ideas how to solve such problem?

SOLVED If someone have such problem use this

$('#sk1').val(something);

instead of this one

$('#sk1').html('something'); (this is bad one)
解决方案

You need to use the .val() method on textarea:

$('#es1').change(function () {
    $('#sk1').val(''); // This line is probably unnecessary...
    var id = $(this).children(":selected").attr("id");
    var idd = id.split('edu');
    var num = idd[1];
    $('#sk1').val(customTextByNumber[num]);
});​

I don't know what customTextByNumber is, but it should work after editing the text in the textarea.

这篇关于Textarea问题,当用户键入,但文字不'改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:40