[英]Trouble calling javascript function from php
我正在嘗試執行以下操作:
<html>
<script type="text/javascript" src="jquery.js"></script>
<a id="random">before</a>
<script>
function test(a)
{
document.getElementById('random').innerHTML="after with variable passed in";
}
function test2()
{
document.getElementById('random').innerHTML="after without variable passed in";
}
</script>
<?php $sample = "hi"; ?>
<button onClick="test(<?php echo $sample;?>)">withVariable</button>
<button onClick="test2()">withoutVariable</button>
</html>
如果單擊“ withoutVariable”按鈕,則函數test2()會被完美調用,因為沒有變量傳遞給它。 但是,如果單擊“ withVariable”按鈕,則由於某種原因永遠不會調用函數test(a)。 有任何想法我在這里做錯了嗎?
由於$sample
是字符串文字,因此需要用''
括起來
<button onClick="test('<?php echo $sample;?>')">withVariable</button>
如果要傳遞字符串變量,則需要在值周圍添加引號,如下所示:
<button onClick="test('<?php echo $sample;?>')">withVariable</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.