简体   繁体   English

在textarea更改 - ajax帖子

[英]On textarea changes - ajax post

I have text area. 我有文字区。 Textarea Content arising from nestable list serealized process. Textarea内容源自可嵌套列表的实现过程。 Textarea content is like: [{"id":1,"children":[{"id":4}]},{"id":2}] Textarea内容如下: [{"id":1,"children":[{"id":4}]},{"id":2}]

Text area: 文字区域:

 <div class="span6">
    <h3>Serialised Output (per list)</h3>
    <textarea id="nestable_list_1_output" class="m-wrap span12"></textarea>
 </div>

Also i have one jquery function: 我还有一个jquery函数:

$(function() { 

function conv(data){
var result=[];
function dfs(node, parent) {
    for(var i in node){
        result.push({id:node[i].id,pid:parent});
        if (node[i]['children']) dfs(node[i]['children'],node[i]['id']);
    }
}
dfs(data, 0);
return result;
}

I want make ajax post every time when values on texarea ​​are changed. 每当texarea上的值发生变化时,我想要发布ajax帖子。

var data     = JSON.stringify(conv($.parseJSON($("textarea#nestable_list_1_output").val())));

$.ajax({
  type: 'POST',
  url: "http://example.com/navigation/save_menu",
  data: 'menu_data=' + JSON.stringify(data),
  dataType: 'json',
  success: console.log('Data Post:' + data),

}); });
How to do that? 怎么做? Many thanks for responses. 非常感谢您的回复。 Karlis. Karlis。

UPDATE UPDATE

Based on great comunity members sugestions event has changed to clik button . 基于伟大的社区成员sugestions 事件已更改为clik按钮 Thanks for sugestions ;) 感谢sugestions;)

You would create a change event: 您将创建一个change事件:

$("#nestable_list_1_output").change(function() {
    var currentText = this.value;
    //AJAX TIME
});

Try this, 尝试这个,

$("#textarea").on('keyup paste', function(e) {
    clearTimeout($(this).data('timeout'));
    $(this).data('timeout', setTimeout(function(){
        alert(e.target.value);
    }, 200));
});

EXAMPLE

This should even work in case of copy paste from keyboard and from mouse 这应该适用于从键盘和鼠标复制粘贴的情况

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

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