我有一个ajax请求将我的邮件模板保存在数据库中。 但是我不能用ajax请求发送我的div?我现在在互联网上看了2个小时,但我找不到解决方案
ajax文件:
function saveEmail(){
var mail3 = $jQ('templateContent');
$jQ.ajax({
method: 'POST',
url: 'ajax/saveMail.php',
data: {mail2: mail3.html(), test: "test"}
}).done(function( data ) {
console.log(data);
});
}
saveMail.php:
echo '<pre>';
print_r($_POST);
echo '</pre>';
exit();
我得到的唯一POST值是测试示例:
<pre>Array
(
[test] => test
)
</pre>
一些有用的信息:
mail3 = `[prevObject: jQuery.fn.jQuery.init[1], context: document, selector: "templateContent", jquery: "1.10.2", constructor: function…]`
答案 0 :(得分:3)
var mail3 = $jQ('templateContent');
应该像是什么
$jQ('.templateContent');
或$jQ('#templateContent');
答案 1 :(得分:1)
您忘记将templateContent
用作元素class
或id
尝试使用.templateContent
或#templateContent
。
因此,您可以使用id
或class
来获取元素的内容。
答案 2 :(得分:0)
以下给出了ajax数据调用中div的内容。注意div的jquery选择器,即var dataToSend = $(“#templateContent”)。text();
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function myFunction() {
var dataToSend = $("#templateContent").text();
alert(dataToSend);
$.ajax({
method: 'POST',
url: 'ajax/saveMail.php',
data: {mail2: dataToSend, test: "test"}
}).done(function( data ) {
console.log(data);
});
}
</script>
</head>
<body>
<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>
<button onclick="myFunction()">Click me</button>
<div id="templateContent"><span><p>This is supposed to be sent in email!</P><span></div>
</body>
</html>