2016-04-21 190 views
0

请找到下面的jQuery代码。完美发送电子邮件,但我在电子邮件中看到大量代码(HTML)。我想将#righthtml()输出转换为text()。我怎样才能做到这一点?将html转换为纯文本jquery .ajax

$(document).ready(function() { 
    $('.confirm_button').click(function() { 
     var student_name = $.trim($('#student_name').val()); 
     var student_contact = $.trim($('#student_contact').val()); 
     var student_email = $.trim($('#student_email').val()); 
     var right = $('#right').html(); 

     if (student_name.length < 3 || student_contact.length < 8 || student_email.length < 4) 
     { 
      $('#sendfail').attr('title', 'Sending Failed!').text('Please Enter valid information. All fields are required').dialog({ 
       buttons: { 
        'Ok': function() { 
         $(this).dialog('close'); 
         $location.reload(true); 
        } 
       }, 
       closeOnEscape: true, 
       draggable: false, 
       resizable: false, 
       modal: true 
      }); 
     } 
     else 
     { 
      var objAjaxRequestData = { 
       'student_name': student_name, 
       'student_contact': student_contact, 
       'student_email': student_email, 
       'right': right 
      }; 
      $.ajax({ 
       type: 'POST', 
       url: 'send-select-university.php', 
       data: objAjaxRequestData, 
       success: function() { 
        $('#sendsuccess') 
          .attr('title', 'Message sent successfully') 
          .text('Your message has been sent. We will be in touch soon') 
          .dialog({ 
           buttons: { 
            'Ok': function() { 
             $(this).dialog('close'); 
             window.location = "http://www.ankooverseas.com"; 
            } 
           }, 
           closeOnEscape: true, 
           draggable: false, 
           resizable: false, 
           modal: true 
          }); 
       }, 
       error: function() { 
        alert('Oops request sending failed. Please check your internet connection and try again'); 
       } 
      }); 
     } 

    }); 
}); 
+0

尝试此解析Html - https://api.jquery.com/jquery.parsehtml/ – hemanjosko

+0

如果您将电子邮件设置为以HTML格式发送,它将使用HTML正确呈现邮件,而不是向您显示源代码。在PHPMailer中,你只需要调用'$ mail-> isHTML();' – Synchro

回答

1

您需要更换

var right=$('#right').html();

var right=$('#right').text();

根据您的意见,我想你会更好地使用下面的代码有在格式化视图中选择的项目:

var right = ""; 
$("#shortlist_univs").children().each(function (i, univ) { // iterate over all universities and add them to the output variable in a list-like view. 
    right += $(univ).text() + "\n" 
}); 
+0

我也试过了。但它看起来像一个段落。这对我来说很难理解。 – user1812111

+0

@ user1812111你想让文本具有某种格式吗? “看起来像一个段落”是什么意思? – ExWei

+0

我必须通过电子邮件发送这个div的文本。所以,如果我使用文本()。我把它看作是一段而不是项目。看看这个http://www.ankooverseas.com/select_university我想抓住选定大学下右栏的文字。 – user1812111