javascript - PHP - 获取 jQuery 生成的 div 内容

标签 javascript php jquery ajax dom

我有以下 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/

相关文章:

javascript - $ ('.classname' ).each(...) 的纯 JavaScript 等价物是什么?

javascript - 删除数组中的奇数

php - ElasticsearchDSL Builder如何创建不应该查询

php - FullCalendar 不显示来自 JSON 事件的时间

javascript - 如何使用jquery通过id获取动态元素值

javascript - 如何检查字符串是否以javascript中的数字开头

javascript - window.location 未正确更新

Javascript 函数不给类

php - mysql - 显示多个类别的产品

javascript - select2 js 插件不起作用