jQuery val 未定义?

发布于 2024-10-17 21:36:41 字数 1786 浏览 2 评论 0原文

我有这个代码:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

马蹄踏│碎落叶 2024-10-24 21:36:42

难道是你忘记在文档就绪函数中加载它了?

$(document).ready(function () {

    //your jQuery function
});

could it be that you forgot to load it in the document ready function?

$(document).ready(function () {

    //your jQuery function
});
等你爱我 2024-10-24 21:36:42

我昨天刚刚在我正在做的一个项目中遇到了这个问题。我是我的具体情况,它不完全是输入的名称,而是 ID 的命名方式

<input id="user_info[1][last_name]" ..... />
var last_name = $("#user_info[1][last_name]").val() // returned undefined

删除括号解决了这个问题:

<input id="user_info1_last_name" ..... />
var last_name = $("#user_info1_last_name").val() // returned "MyLastNameValue"

无论如何,对于某些人来说可能是理所当然的,但如果这对其他人有帮助......就这样吧!

:-)
- 德鲁

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.

<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

风吹雨成花 2024-10-24 21:36:42

您可能忘记用 $() 包装您的对象

  var tableChild = children[i];
  tableChild.val("my Value");// this is wrong 

,正确的是

$(tableChild).val("my Value");// this is correct

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
浸婚纱 2024-10-24 21:36:42

尝试: $('#editorTitle').attr('value')

try: $('#editorTitle').attr('value') ?

丑丑阿 2024-10-24 21:36:42

就我而言,我有两个同名的函数。

In my case I had 2 functions with same name.

浮光之海 2024-10-24 21:36:41

您可能在 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.

沫离伤花 2024-10-24 21:36:41

您应该在文档准备好之后调用事件,如下所示:

$(document).ready(function () {
  // Your code
});

这是因为您试图在浏览器呈现元素之前对其进行操作。

因此,在您发布的情况下,它应该看起来像这样

$(document).ready(function () {
  var editorTitle = $('#editorTitle').val();
  var editorText = $('#editorText').html();
});

希望它有帮助。

提示:始终将 jQuery 对象保存在变量中以供以后使用,并且只有真正需要在文档加载后运行的代码才应放入 read() 函数中。

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.

丢了幸福的猪 2024-10-24 21:36:41

这很愚蠢,但可供将来参考。
我确实将所有代码放入:

$(document).ready(function () {
    //your jQuery function
});

但它仍然无法正常工作,并且返回 undefined 值。
我检查了我的 HTML DOM

<input id="username" placeholder="Username"></input>

,发现我在 jQuery:

var user_name = $('#user_name').val();

Making it:

var user_name = $('#username').val();

解决了我的问题中引用了它是错误的。

因此,最好检查一下以前的代码。

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

<input id="username" placeholder="Username"></input>

and I realised that I was referencing it wrong in 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文