我有这个代码:
<input type="hidden" id="editorTitle" name="title" value="home">
<textarea name="text" id="editorText"></textarea>
但是当我编写 $('#editorTitle').val()
和 $('#editorText').html()
时,$( '#editorTitle').val()
是 'undefined' 而 $('#editorText').html()
是空的?
怎么了?
编辑:
这是我的完整代码:
function openEditor() {
$("#editor").show().animate({ width: 965, height: 380 }, 1500);
$("#editor textarea").show();
}
function closeEditor() {
$("#editor").animate({ width: 985, height: 1 }, 1500, function () {
$("#editor").hide();
$("#editor textarea").hide();
});
}
function setedit() {
$.ajax({
type: "POST",
url: "engine.php",
data: "title=" + $('#editorTitle').attr('value') + "&text=" + $('#editorText').html(),
beforeSend: function () {
$('#mainField').html('<img src="data/images/loader.gif" alt="Loading...">');
},
success: function (msg) {
alert(msg);
closeEditor();
search();
}
});
}
function search() {
$('#title').val($('#search').val());
$.get('engine.php?search=' + $('#search').val(), function (data) {
$('#mainField').html(data);
});
$.get('engine.php?raw=true&search=' + $('#search').val(), function (data2) {
$('#editorText').html(data2);
});
$.get('engine.php?title=true&search=' + $('#search').val(), function (data2) {
$('#h1').html(data2); // Row 152
$('#editorTitle').html(data2);
});
}
$(document).ready(function () {
$("#ready").html('Document ready at ' + event.timeStamp);
});
但是怎么了?!?!
最佳答案
您可能在 HTML 之前包含了您的 JavaScript:example .
您应该在窗口加载时执行您的 JavaScript(或者更好,当 DOM 完全加载时),或者您应该在您的 HTML 之后包含您的 JavaScript:example .
关于javascript - jQuery val 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5006045/