jQuery val是不确定的? [英] jQuery val is undefined?
问题描述
我有这样的代码:
< input type =hiddenid =editorTitlename =title值= 家 >
但是当我写 $('#editorTitle')。val()
和 $('#editorText')。html()
, $('#editorTitle')。val()
是'undefined'和 $('#editorText')。html()
是空的吗?
有什么不对?
编辑:
这是我的完整代码:
$ p $ function openEditor(){
$(#editor)。show ).animate({width:965,height:380},1500);
$(#editor textarea)。show();
}
函数closeEditor(){
$(#editor)。animate({width:985,height:1},1500,function(){
$(#editor)。hide();
$(#editor textarea)。hide();
}); $ {
}
函数setedit(){
$ .ajax({
类型:POST,
url:engine.php,
data:title =+ $('#editorTitle')。attr('value')+& text =+ $('#editorText')。html(),
beforeSend:函数(){
$('#mainField')。html('< img src =data / images / loader.gifalt =正在加载...>');
},
success:function(msg){
alert(msg);
closeEditor();
search();
}
});
function search(){
$('#title')。val($('#search')。val());
$ .get('engine.php?search ='+ $('#search')。val(),function(data){
$('#mainField')。 html(data);
});
$ b $ .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); //第152行
$('#editorTitle')。html(data2);
});
$ b $(document).ready(function(){
$(#ready)。html('document ready at'+ event.timeStamp);
});
但有什么不对?!?!
您可能在HTML之前包含了您的JavaScript :示例。
您应该在窗口加载时执行JavaScript(或者更好,当DOM完全加载时),或者您应该在您的HTML <示例之后包含您的JavaScript 。
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?!?!
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 val是不确定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!