开发者

Why $('#id').val() not working while document.getElementById('id').value works perfect?

开发者 https://www.devze.com 2023-02-19 22:48 出处:网络
$(\'#id\').val() = $.cookie(\"name\"); - not works, nothing cha开发者_如何学Cnges document.getElementById(\'id\').value = $.cookie(\"name\"); - works fine

$('#id').val() = $.cookie("name"); - not works, nothing cha开发者_如何学Cnges

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

Why?


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 comparable 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号