jQuery val 未定义?
我有这个代码:
<input type="hidden" id="editorTitle" name="title" value="home">
<textarea name="text" id="editorText"></textarea>
但是当我写 $('#editorTitle').val()
和 $('#editorText').html()
, $('#editorTitle').val()
为“未定义”且 $('#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);
});
但是出了什么问题?!?!
I have this code:
<input type="hidden" id="editorTitle" name="title" value="home">
<textarea name="text" id="editorText"></textarea>
But when i write $('#editorTitle').val()
and $('#editorText').html()
, $('#editorTitle').val()
is 'undefined' and $('#editorText').html()
is empty?
What is wrong?
Edit:
Here is my full code:
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);
});
But what is wrong?!?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
难道是你忘记在文档就绪函数中加载它了?
could it be that you forgot to load it in the document ready function?
我昨天刚刚在我正在做的一个项目中遇到了这个问题。我是我的具体情况,它不完全是输入的名称,而是 ID 的命名方式。
删除括号解决了这个问题:
无论如何,对于某些人来说可能是理所当然的,但如果这对其他人有帮助......就这样吧!
:-)
- 德鲁
I just ran across this myself yesterday on a project I was working on. I'm my specific case, it was not exactly what the input was named, but how the ID was named.
Removing the brackets solved the issue:
Anyway, probably a no brainer for some people, but in case this helps anyone else... there you go!
:-)
- Drew
您可能忘记用
$()
包装您的对象,正确的是
you may forgot to wrap your object with
$()
and the ccorrect one is
尝试:
$('#editorTitle').attr('value')
?try:
$('#editorTitle').attr('value')
?就我而言,我有两个同名的函数。
In my case I had 2 functions with same name.
您可能在 HTML 之前添加了 JavaScript:示例。< /strong>
您应该在窗口加载时执行您的 JavaScript(或者更好的是,当 DOM 完全加载时),或者您应该在 HTML 之后包含您的 JavaScript:示例。
You probably included your JavaScript before the HTML: example.
You should either execute your JavaScript when the window loads (or better, when the DOM is fully loaded), or you should include your JavaScript after your HTML: example.
您应该在文档准备好之后调用事件,如下所示:
这是因为您试图在浏览器呈现元素之前对其进行操作。
因此,在您发布的情况下,它应该看起来像这样
希望它有帮助。
提示:始终将 jQuery 对象保存在变量中以供以后使用,并且只有真正需要在文档加载后运行的代码才应放入 read() 函数中。
You should call the events after the document is ready, like this:
This is because you are trying to manipulate elements before they are rendered by the browser.
So, in the case you posted it should look something like this
Hope it helps.
Tips: always save your jQuery object in a variable for later use and only code that really need to run after the document have loaded should go inside the ready() function.
这很愚蠢,但可供将来参考。
我确实将所有代码放入:
但它仍然无法正常工作,并且返回
undefined
值。我检查了我的 HTML DOM
,发现我在 jQuery:
Making it:
解决了我的问题中引用了它是错误的。
因此,最好检查一下以前的代码。
This is stupid but for future reference.
I did put all my code in:
But still it wasn't working and it was returning
undefined
value.I check my HTML DOM
and I realised that I was referencing it wrong in jQuery:
Making it:
solved my problem.
So it's always better to check your previous code.