问题描述:

I'm working on a piece of JavaScript. I'm completely new to JS, But I have a group of checkboxes part of the domainchkb class and with the name, all with different values. I am trying to group those into an array to format into JSON. When executing the code the debugger stops at return this.parent().text();. What am I doing wrong here? Any help would be appreciated.

function format4Cart(){

var domains;

var values = $('input[name=domainchkb]:checked').map(function(){

return this.parent().text();

}).get();

domains = "{\"type\" : \"single\", \"data\" : [";

$.each(values, function(domainIndex,selected_domain){

domains += "{\"id\" : \""+selected_domain+"\", \"domain\" : \""+whois_response.data.domain+"\"},";

});

$(domains).text().replace(/(\s+)?.$/,"");

domains += "]}";

domains=encodeURIComponent(domains);

ajaxAdd2Cart(domains);

}

网友答案:

You are trying to call a jQuery method, .parent(), on a DOM element. In order to use a jQuery method on the element, you need to wrap it in a jQuery call:

var values = $('input[name=domainchkb]:checked').map(function(){ 
        return $(this).parent().text(); 
}).get(); 
网友答案:

If you're using the debugger already just look at it see what values are there for this and other variables. Might be something you do not expect. I think the issue is that you're trying to use this while you should use function parameter:

var values = $('input[name=domainchkb]:checked').map(function(checkbox){
        return checkbox.parent().text();
}).get();    
相关阅读:
Top