简体   繁体   English

JQUERY-键入textarea后。更改选择菜单不会执行任何操作

[英]JQUERY - after typing in textarea .change of select menu doesnt do anything

I am little bit confused of that what happened because it's illogical for me. 我对发生的事情感到困惑,因为这对我来说是不合逻辑的。 What's the point? 重点是什么?

I have such a HTML: 我有这样的HTML:

<select id="id_number">
    <option value="0">option 1</option>
    <option value="1">option 2</option>
    <option value="2">option 3</option>

</select>
<br /><br />
<textarea id="description" placeholder="whatever">test1</textarea>

and such jquery 和这样的jQuery

$("#id_number").change(function() {
    $("#description").text('123');  // test function
});

When I load the page i should have select menu with 3 options and textarea with 'test1' text inside. 当我加载页面时,我应该选择菜单,其中包含3个选项,而textarea中包含“ test1”文本。 And i do have. 而且我有。 Now, when i change to any option, textarea changes to '123 as intended with test function. 现在,当我更改为任何选项时,textarea会按照测试功能的预期更改为'123。 Unfortunatelly when I type any text in textarea from my keyboard, and again choose option from menu it is not putting '123' to textarea - i do not know why but nothing happens as if typing anything in textarea changed in some way state of textarea and jquery could change it anymore. 不幸的是,当我从键盘上输入文本区域中的任何文本,然后再次从菜单中选择选项时,它没有将'123'放置到文本区域中-我不知道为什么,但是什么也没发生,好像在文本区域中键入任何内容都以某种方式改变了文本区域并jQuery可以改变它了。 Do you have any ideas how to change textarea text each time i change option in select menu? 您是否有任何想法,每次我在选择菜单中更改选项时如何更改textarea文本?

text() just change the value of the DOM element, the text area in this case, but it doesn't change the actual value. text()仅更改DOM元素的值(在这种情况下为文本区域),但不更改实际值。 In order to do that, you have to use val() . 为此,您必须使用val() You can find more information about it on the jQuery documentation page: http://api.jquery.com/text/ . 您可以在jQuery文档页面上找到有关它的更多信息: http : //api.jquery.com/text/

You should try this for select any option: 您应该尝试此操作以选择任何选项:

$('#id_number').live('change', function(){ $('#id_number')。live('change',function(){

var id_number_value=$('#id_number').val(); 
$('#description').val('');
var description_value=$('#description').val();
if(id_number_value=='' || id_number_value=='null')
    $('#description').val(id_number_value+""+description_value);
else 
    $('#description').val(id_number_value+" ");

}); });

I would suppose that it is a problem with where you have your jQuery statement in the HTML itself. 我想这是您在HTML本身中拥有jQuery语句的问题。 Cause the code works for me on JSFiddle... Look here http://jsfiddle.net/AKuWd/ 因为代码在JSFiddle上对我有用...请看这里http://jsfiddle.net/AKuWd/

However, when you change to "nowrap (body)" it does not work... 但是,当您更改为“ nowrap(body)”时,它将不起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM