我有一个跨度,它看起来像这样:
<span class="price" id="product-price-167">€47.00</span>
47.00是使用JavaScript动态设置的。现在,当用户单击按钮时,就会触发对PHP脚本的请求。我需要那个剧本里的47.00。我怎么能那样做?我知道JS是前端,PHP是服务器端,但有办法吗?
谢谢:)
解决方案:我正在使用隐藏字段,并使用读取该字段
$price = $_POST["price"];
您可以使用jquery来检索元素的值:
var value = $('#product-price-167').text();
我认为这应该给你47.00。
使用JQuery:
你可以得到跨度内的价格:
var price = $('#product-price-167').text();
然后,你可以相应地解析价格以去掉欧元符号。在不知道按钮的确切位置或工作方式的情况下,很难告诉你如何将其添加到PHP脚本中。
请注意,如果这是一个严肃的应用程序,则不应解析为Float:使用
var parsedNumber = Number(price.replace(/[^0-9'.]+/g,""));
您可以使用jquery轻松地使用ajax 将该值发布到php页面
<script type="text/javascript">
// i m assuming that 167 in id product-price-167 comes from database in a loop
function buttonClick(pid){
var price=$("#product-price-"+pid).text();
$.ajax({
type : "POST",
data : "price="+price,
url : "yourphppage.php",
success :function(data){
//return something from php page
}
});
}
</script>
和你的输入按钮
<input type="button" onclick="buttonClick('167')" />
var path = 'path-to-script.php';
var data = 'price='+Number($('#product-price-167').replace(/[^0-9'.]+/g,"")); // to remove redundant [currency] characters
$.post(path, data, function(returnText){
// do stuff here
});
应该可以。