简体   繁体   English

Jquery 事件单击 html() 仅有效一次

[英]Jquery event click html() only works once

I have a function that when clicking on the element should enter a textarea value, but the function works only by clicking once and then does not work anymore, unless loading the page.我有一个 function ,单击该元素时应输入一个 textarea 值,但 function 仅通过单击一次即可工作,然后不再工作,除非加载页面。 How to make the function work every time I click on the element?每次单击元素时,如何使 function 工作?

I made the function in javascript, but this only works in Firefox and does not work in chrome, so I gave up and tried jquery I made the function in javascript, but this only works in Firefox and does not work in chrome, so I gave up and tried jquery

$("#element").click(function(){
    $("#nameidtextarea").html("<br>");
});

It was expected that when clicking on #element the value "<'br'>" will be inserted into the textarea预计单击#element 时,值“<'br'>”将插入到文本区域中

The HTML content of a textarea element is the default value, so using it will only change the value if it hasn't been changed by the user. textarea 元素的 HTML 内容是默认值,因此使用它只会在用户未更改的情况下更改值。

Do not use html() to modify the value of a form control.不要使用html()来修改表单控件的值。 Use val() .使用val()


You are currently setting the value to <br> .您当前正在值设置为<br> That's a replace action, not an insert action.那是替换操作,而不是插入操作。

If you want to insert the data and not erase the existing data, then you will need to read the current value, modify it, then set the new value back.如果要插入数据而不删除现有数据,则需要读取当前值,修改它,然后设置新值。


$("#element").click(function(){
    const old_value = $("#nameidtextarea").val();
    const new_value = old_value + "<br>";
    $("#nameidtextarea").val(new_value);
});

Thanks everyone!感谢大家!

$("#nameidtextarea").val($("#nameidtextarea").val()+$("#nameidtextarea").html()+"<br>");

Now works for me现在为我工作

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

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