简体   繁体   中英

jQuery array populating mailto link adding commas

I have a list of links to pdfs. Users can check a checkbox for each pdf, and then submitting the form will launch an email (using mailto:) with the items they have selected.

Everything works fine, except in the body of the email, the selected pdfs array are separated by a comma, so the comma is appearing in the email body.

Can anyone please help me get rid of the comma that separates them?

Tina

http://tinyurl.com/7v4deh2

Update your script like below and let me know if it works,

$('#send-email').submit(function(){
    var selectedpdfs = '';
    $('#send-email input:checkbox:checked').each(function(i){
        // All selected pdfs: gets link's text and link's url
        selectedpdfs += $(this).prev().text() + '%0a' + $(this).prev().attr('href') + '%0a%0a'
    });
    //alert(selectedpdfs);
    window.location.href = 'mailto:?subject=Materials&body='+selectedpdfs
    return false;
});

Note: I modified selectedpdfs to a string object and changed it to string concatenation.

Array to String -> will return you a comma separated list of string.

use join() on the array to concatenate them into a string, you may speicfy which character to delimit.

eg selectedpdfs.join(' ') will return a string delimited by a space.

see http://jsfiddle.net/qiao/HmWf3/1/ for a live demo

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