[英]jQuery val is undefined?
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()
和$('#editorText').html()
时, $('#editorTitle').val()
是 'undefined' 和$('#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?!?!但是怎么了?!?!
You probably included your JavaScript before the 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 after your HTML: example .您应该在窗口加载时执行 JavaScript(或者更好的是,在 DOM 完全加载时),或者您应该在 HTML之后包含 JavaScript: example 。
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() 函数中。
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.但它仍然没有工作,它返回
undefined
的值。 I check my HTML DOM我检查了我的 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.所以最好检查你以前的代码。
could it be that you forgot to load it in the document ready function?可能是您忘记将其加载到文档就绪功能中吗?
$(document).ready(function () {
//your jQuery function
});
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
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.
我是我的具体情况,它不完全是输入的名称,而是 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 :-) - 德鲁
试试: $('#editorTitle').attr('value')
?
Check if id
containing the character which is not allowed.检查
id
是否包含不允许的字符。
ID
tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). ID
标记必须以字母 ([A-Za-z]) 开头,后跟任意数量的字母、数字 ([0-9])、连字符 ("-")、下划线 ("_")、冒号(":") 和句点 (".")。
In my case, I am generating id in HTML dynamically with some input values.就我而言,我使用一些输入值在 HTML 中动态生成 id。 And so, it was containing space character.
因此,它包含空格字符。 When I removed, the error gone.
当我删除时,错误消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.