最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why $('#id').val() not working while document.getElementById('id').value works perf

programmeradmin3浏览0评论

$('#id').val() = $.cookie("name"); - not works, nothing changes

document.getElementById('id').value = $.cookie("name"); - works fine

Why?

$('#id').val() = $.cookie("name"); - not works, nothing changes

document.getElementById('id').value = $.cookie("name"); - works fine

Why?

Share Improve this question edited Mar 27, 2011 at 21:54 Diodeus - James MacFarlane 114k33 gold badges163 silver badges180 bronze badges asked Mar 27, 2011 at 21:52 AlexAlex 154 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

You are not directly accessing the elements value. The left hand side of your expression is a getter which evaluates to a literal. So the expression is parable to something like 5 = 10, which obviously cant work

$('#id').val() = $.cookie("name");

val() is overloaded, and by giving it an argument you can reassign

$('#id').val($.cookie("name"));

try to use

$('#id').val($.cookie("name")); 

You are using the getter version of val() when you call it with no arguments.

Use $('#id').val($.cookie("name")); instead.

$('#id').val($.cookie("name"));

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论