[英]How to change an <input> field's VALUE attribute in real time with JavaScript?
[英]How do I a use Javascript or ajax to inject or add a the value of an input field in a textfield in real time?
我有一个id为#image_tag_list_tokens的text_field,文本字段在图像形式中显示如下:
= f.text_area :tag_list_tokens, label: "Tags (optional) ->", data: {load: @image_tags }, label: "Tags"
我有一个输入字段和一个按钮,如下所示:
<input type="text" name="myNewTag" id="my_new_tag">
<button name="meNewTagButton" type="button" id="createMy_new_tag">Create new tag</button>
当用户在输入字段中键入新标记时,我想获取该新标记并将其添加到:tag_list_tokens的文本区域内,而无需重新加载页面。 tag_list_tokens文本字段的值具有以下格式:
"old_tag1,old_tag2,'new_tag1','new_tag2'"
// add event listener for button click $("#createMy_new_tag").on("click", function(){ // get the text in the input var new_tag = $("#my_new_tag").val(), // grab the textarea textarea = $("#image_tag_list_tokens") // append the input to the current text textarea.val(textarea.val() + ", " + new_tag) // reset the input $("#my_new_tag").val("") })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="image_tag_list_tokens">blah</textarea> <input type="text" name="myNewTag" id="my_new_tag"> <button name="meNewTagButton" type="button" id="createMy_new_tag">Create new tag</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.