Javascript jQuery val 未定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5006045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery val is undefined?
提问by Thew
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?
但是当我写$('#editorTitle').val()
and 时$('#editorText').html()
,$('#editorTitle').val()
是“未定义”并且$('#editorText').html()
是空的?
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?!?!
但是怎么了?!?!
回答by Aron Rotteveel
You probably included your JavaScript beforethe HTML: example.
您可能在 HTML: example之前包含了您的 JavaScript 。
You should either execute your JavaScript when the window loads (or better, when the DOM is fully loaded), or you should include your JavaScript afteryour HTML: example.
您应该在窗口加载时执行您的 JavaScript(或者更好,当 DOM 完全加载时),或者您应该在HTML之后包含您的 JavaScript :example。
回答by Jonathan
You should call the events after the document is ready, like this:
您应该在文档准备好后调用事件,如下所示:
$(document).ready(function () {
// Your code
});
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
所以,如果你发布它应该看起来像这样
$(document).ready(function () {
var editorTitle = $('#editorTitle').val();
var editorText = $('#editorText').html();
});
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.
提示:始终将您的 jQuery 对象保存在一个变量中以备后用,并且只有真正需要在文档加载后运行的代码才应该放在 ready() 函数中。
回答by fWd82
This is stupid but for future reference. I did put all my code in:
这很愚蠢,但供将来参考。我确实把我所有的代码都放在了:
$(document).ready(function () {
//your jQuery function
});
But still it wasn't working and it was returning undefined
value.
I check my HTML DOM
但它仍然无法正常工作,并且正在返回undefined
值。我检查我的 HTML DOM
<input id="username" placeholder="Username"></input>
and I realised that I was referencing it wrong in jQuery:
我意识到我在 jQuery 中引用它是错误的:
var user_name = $('#user_name').val();
Making it:
进行中:
var user_name = $('#username').val();
solved my problem.
解决了我的问题。
So it's always better to check your previous code.
所以最好检查你以前的代码。
回答by Marcel Colomb
could it be that you forgot to load it in the document ready function?
可能是您忘记在文档就绪功能中加载它吗?
$(document).ready(function () {
//your jQuery function
});
回答by null
try: $('#editorTitle').attr('value')
?
试试:$('#editorTitle').attr('value')
?
回答by Drew
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 howthe ID was named.
我昨天刚刚在我正在做的一个项目中遇到了这个问题。我为我的特定情况下,它不完全是输入被命名,但如何将ID命名。
<input id="user_info[1][last_name]" ..... />
var last_name = $("#user_info[1][last_name]").val() // returned undefined
Removing the brackets solved the issue:
去掉括号解决了这个问题:
<input id="user_info1_last_name" ..... />
var last_name = $("#user_info1_last_name").val() // returned "MyLastNameValue"
Anyway, probably a no brainer for some people, but in case this helps anyone else... there you go!
无论如何,对某些人来说可能是没有道理的,但万一这对其他人有帮助......你去吧!
:-) - Drew
:-) - 德鲁
回答by Basheer AL-MOMANI
you may forgot to wrap your object with $()
你可能忘记用 $()
var tableChild = children[i];
tableChild.val("my Value");// this is wrong
and the ccorrect one is
而正确的是
$(tableChild).val("my Value");// this is correct