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

javascript - Error Cannot set property 'value' of undefined - Stack Overflow

programmeradmin2浏览0评论
$(function() {
    $.get('http://localhost/rand.php', function(data){
        document.getElementsByName('login')[0].value='data';
        console.log(data);
    });
});

what is the problem in these code. they are giving me error like "background.js:3 Uncaught TypeError: Cannot set property 'value' of undefined".

$(function() {
    $.get('http://localhost/rand.php', function(data){
        document.getElementsByName('login')[0].value='data';
        console.log(data);
    });
});

what is the problem in these code. they are giving me error like "background.js:3 Uncaught TypeError: Cannot set property 'value' of undefined".

Share Improve this question edited Dec 26, 2015 at 17:29 jianweichuah 1,4171 gold badge11 silver badges22 bronze badges asked Dec 26, 2015 at 8:39 aarif khanaarif khan 211 silver badge2 bronze badges 1
  • You could try document.getElementsByTagName('input')... – Bahramdun Adil Commented Dec 26, 2015 at 8:42
Add a ment  | 

1 Answer 1

Reset to default 3

Do you have name=login element in your HTML page, if no then you cannot get the element and its value.

You can have something like this:

<input type="text" name="login" value="OK">

Then you can get this element by name and then get the value.

Below is what I tried:

function onLoad() {
    var value= document.getElementsByName('login')[0].value;
    alert("value="+value); // value=OK
}
发布评论

评论列表(0)

  1. 暂无评论