简体   繁体   中英

jQuery strange “undefined” reaction while getting value

I have a problem with the variable. So i'm making post form with jQuery. The input that i'm trying to get the info from is hidden:

<input id="company_id" name="company_id" type="hidden" value="4">

And the jQuery code is like this:

if (button == 'rem') {
    var term = $("#userlist").val();
    var comid = $( "#company_id").val();
    $.post('/delfromgroup', { id: term, cid: comid }, function(html){ 
        console.log(comid);
        $("#list").fadeOut("slow",function() { 
            $("#list").html($(html).filter("#error")); 
            $("#list").fadeIn("slow"); 
        });
    });
}

The first time, when i hit the button, its fine... the console.log says the right number, but after the first time, everytime when i press the button, the value is "undefined". Why can't read the html code correctly after the first time? Any suggestions? Thank you :)

If your hidden field company_id would be inside #list then it will be removed by the jQuery code ( fadeOut() ) you have used.

So when you call it next time, you are getting undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM