[英]HTML div with jquery on.click not working as expected
美好的一天,我在下面寫了一個 html div。
.SubmitOrder { height: 60px; width: 200px; margin-top: 10px; margin-right: 30px; float: right; background: #C40514; text-align: center; display: block; }
<head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <div class="SubmitOrder"> <?php echo "Total: ".$totalPrice."<br> Pay Now!" ?> <script> $('.SubmitOrder').on('click', function() { window.location.replace(<?php echo "home.php?account_id=$accountid" ?>); }) </script> </div>
但是在單擊 div 時,它不會將我重定向到鏈接。 難道我做錯了什么?
最好使用ready
函數將腳本放在head
部分中,以確保加載腳本中的所有目標元素。
注意:您還必須將新鏈接<?php echo "home.php?account_id=$accountid" ?>
用單引號''
括起來。
window.location.replace('<?php echo "home.php?account_id=$accountid" ?>');
.SubmitOrder { height: 60px; width: 200px; margin-top: 10px; margin-right: 30px; float: right; background: #C40514; text-align: center; display: block; }
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> $(function() { $('.SubmitOrder').on('click', function() { window.location.replace('<?php echo "home.php?account_id=$accountid" ?>'); }); }); </script> </head> <body> <div class="SubmitOrder"> Total: 1000$ <br> Pay Now! </div> </body> </html>
試試這個 javascript
<div class="SubmitOrder">
<?php echo "Total: ".$totalPrice."<br> Pay Now!" ?>
</div>
<script>
$('.SubmitOrder').on('click', function() {
window.location.replace(<?php echo "home.php?account_id=$accountid" ?>);
})
</script>
使用類SubmitOrder
將腳本標簽移出 div
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.