2016-04-21 205 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