[英]How to call javascript function in wordpress using inline function call?
我已經在wordpress管理區域中設置了我的插件。 我有一個存儲用戶信息的表格。 在我的文件輸入類型中,我具有對鏈接到我的自定義javascript的javascript函數的調用。 這是一行: <li>Category Image:<input type="file" name="category[image]" id="cat_image" onchange="javascript:ImageUpload()" /></li>
。 但是,它返回一個錯誤,提示未定義的函數ImageUpload()
,肯定無法找到該函數。 我在想的是可能需要一個wordpress函數來調用它,還是我需要首先設置? 但是我已經鏈接了我的自定義js,並且可以正常工作。 問題是如何從管理員區域的wordpress php文件中調用某些js函數? 我希望你們能理解這一點。 謝謝。 這是我的代碼:
plugin_file.php
<form method="post" name="new_category" id="product_category" enctype="multipart/form-data">
<ul class="add_prod">
<li>Category Title:<input type="text" name="category[title]" id="cat_title" value="" placeholder="Title" /> </li>
<li>Category Description:<textarea rows="4" cols="40" name="category[description]"></textarea></li>
<li>Category Image:<input type="file" name="category[image]" id="cat_image" onchange="javascript:ImageUpload()" /></li>
<li><input type="submit" name="submit" id="submit" value="Submit details" class="btn-submit"/></li>
</ul>
</form>
js文件:
function ImageUpload() {
$("#return").show();
$("#return").html('');
$("#return").html('<img src="images/load2.gif" alt="Uploading...."/> wait, uploading...');
$("#imageform").ajaxForm({
target: '#return',
success: function() {
$("#return").fadeOut(10000);
}
}).submit();
}
確保在調用之前加載了js文件。
你可以做這樣的事情
<?php
add_action('wp_head','js_call'); // make sure js is loaded in head
function js_call() { ?>
<script>
function ImageUpload() {
jQuery("#return").show();
jQuery("#return").html('');
jQuery("#return").html('<img src="images/load2.gif" alt="Uploading...."/> wait, uploading...');
jQuery("#imageform").ajaxForm({
target: '#return',
success: function() {
jQuery("#return").fadeOut(10000);
}
}).submit();
</script>
<?php } ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.