[英]Javascript function onclick is not defined
我正在嘗試在按鈕單擊時啟動一些操作,因此我定義了一個函數並傳遞了一個 php 值作為我從控制器獲取的參數,並且我願意使用此參數值通過 ajax 將一些參數傳遞給另一個控制器。
所以我正在設置一個功能,在一個按鈕中提供午餐功能
<button type="" onclick="CreateCommende('<?php echo $username; ?>')" class="btn btn-primary pull-right"><i class="fa fa-check"></i> Valider</button>
然后我想看看它是否在腳本部分檢索正確的 PHP 值:
function CreateCommende(username)
{
var name = username;
console.log(name);
}
;
但是當我在控制台中傳遞CreateCommende('admin')
,我得到了這個
未捕獲的 ReferenceError:CreateCommende 未在 :1:1 處定義
有什么問題?
確保腳本位於結束</body>
標記之前
<script>
function CreateCommende(username)
{
var name = username;
console.log(name);
}
;
</script>
</body>
javascript 函數未加載到文件或腳本標記中
<script type="text/javascript">
function CreateCommende(username)
{
var name = username;
console.log(name);
};
</script>
<button type="" onclick="CreateCommende('<?php echo $username; ?>')" class="btn btn-primary pull-right"><i class="fa fa-check"></i> Valider</button>
為避免損壞的標記,您可以使用https://www.php.net/manual/en/function.addslashes.php轉義您的輸出
<?php echo addslashes($username); ?>
我通過將函數代碼放在外面解決了這個問題:
$(document).ready(function() {}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.