Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery.text()将不会更新_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery.text()将不会更新

Javascript jQuery.text()将不会更新,javascript,jquery,html,Javascript,Jquery,Html,我的问题是,当触发“keydown”事件时,textarea的文本不会更新为textarea中当前的文本,而只是textarea中最初的文本。请查看帮助我描述问题的。单击单词测试并尝试编辑出现的文本区域。请注意,警报仍然只会导致“测试”,而不是更改为什么 <script> $('#content').click(function () { var content = $('#content').text(); $('#content').html("<texta

我的问题是,当触发“keydown”事件时,textarea的文本不会更新为textarea中当前的文本,而只是textarea中最初的文本。请查看帮助我描述问题的。单击单词测试并尝试编辑出现的文本区域。请注意,警报仍然只会导致“测试”,而不是更改为什么

<script>
$('#content').click(function () {
    var content = $('#content').text();
    $('#content').html("<textarea id='text'>" + content + "</textarea>");
    $('textarea').width($(window).width()).height($(window).height());
    $("#content").unbind('click');
});

$("body").delegate("textarea", "keydown",function(e){
    myAjaxFunction($('textarea').text());
    alert($('textarea').text());
});

function myAjaxFunction(value) {
$.post("update.php?filename=" + filename, { content: value });
}
</script>

$(“#内容”)。单击(函数(){
var content=$('#content').text();
$('#content').html(“+content+”);
$('textarea').width($(window.width()).height($(window.height());
$(“#内容”)。解除绑定(“单击”);
});
$(“body”).delegate(“文本区域”,“按键”,函数(e){
myAjaxFunction($('textarea').text());
警报($('textarea').text());
});
函数myAjaxFunction(值){
$.post(“update.php?filename=“+filename,{content:value}”);
}
一些更改:

1) 您需要使用
on()
而不是
delegate()
,因为从jQuery 1.7开始,
.delegate()
已被
.on()
方法取代

2) 使用
keyup
而不是
keydown
事件

3) 使用获取文本区域的值

$("body").on("keyup", "textarea",function(e){
    alert($('textarea').val());
}); 

据报道,它说:

.text()方法不能用于表单输入或脚本。设置或 获取输入或textarea元素的文本值,使用.val() 方法。要获取脚本元素的值,请使用.html()方法

所以只需将text()替换为val(),检查

$(“#内容”)。单击(函数(){
var content=$('#content').text();
$('#content').html(“+content+”);
$('textarea').width($(window.width()).height($(window.height());
$(“#内容”)。解除绑定(“单击”);
});
$(“body”).delegate(“文本区域”,“按键”,函数(e){
警报($('textarea').val());
});
函数myAjaxFunction(值){
$.post(“update.php?filename=“+filename,{content:value}”);
}

页面上有多少
元素?尝试
myAjaxFunction($('#text').text())美女。在你更新帖子之前,我照你说的做了,效果非常好。多谢各位@用户2416047可能应该将其设置为可接受的答案:)
$('#content').click(function () {
    var content = $('#content').text();
    $('#content').html("<textarea id='text'>" + content + "</textarea>");
    $('textarea').width($(window).width()).height($(window).height());
    $("#content").unbind('click');
});

$("body").delegate("textarea", "keydown",function(e){
    alert($('textarea').val());
});

function myAjaxFunction(value) {
$.post("update.php?filename=" + filename, { content: value });
}