Textarea issue when user types, but text doesnt' change

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

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)

回答1:

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

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



回答2:

can you try this:

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

here you can see http://jsfiddle.net/tH8N5/



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!