我有以下 div,其中有一个由 jQuery 生成的数字:
HTML:
<td id="Total" class="total">
</td>
jQuery:
var sum = 0;
$(".item-price").each(function() {
var NewQuoteTotal = $(this).html().replace('£', '')
sum += parseInt(NewQuoteTotal);
})
console.log(sum)
$(".total").text('£' + sum)
我需要从该元素获取生成的数字,以便我可以在 PHP 中使用它来在其他地方显示。
我干预了 DOM(我不知道):
$dom = new domDocument;
$html = file_get_contents("FILELOCATION"); //I don't use FILELOCATION, that's just a placeholder for StackOverflow.
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$thePrice = $dom->getElementById("Total");
if(!$thePrice) {
die("Element Not Found!");
}
echo "Element is there!";
$xpath = new DOMXPath($dom);
$divContent = $xpath->query('//td[id="Total"]');
echo $divContent;
并尝试使用 jQuery $.post() 包含数字的变量,但没有成功。
最佳答案
$.post('/path/to/my/php/script.php', {total: $('#Total').text()}, function(){
//what to do with the returned data from the server
});
在 script.php
中(如上所述):
$total = isset($_POST['total']) ?: false;
if($total !== false):
//you can use $total now
endif;
关于javascript - PHP - 获取 jQuery 生成的 div 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24490116/