我尝试使用Ajax做一件小事。我只是想换一个div。
这就是我到目前为止所做的。
我使用创建了管理菜单add_menu_page
.
然后,我向该页面添加一个带有文本字段的表单。单击submit按钮时,我只需要替换一个div。但它没有起作用。当我在浏览器中检查控制台时,它会显示Uncaught TypeError: Cannot read property \'ajax\' of undefined
<小时>
function myajaxfunction() {
$.ajax({ //ajax request
url: ajaxurl,
data: {
\'action\':\'rs_like_request\',
\'post_type\' : $(\'#post_type\').val() // value of text box having id "post_type"
},
success:function(data) { //result
$(".showdiv").html(data); //showdiv is the class of the div where we want to show the results
},
error: function(errorThrown){
console.log(errorThrown);
}
});
}
<小时><form action="" method="post">
<label>Write post type : </label>
<input type="text" name="post_type" id="post_type" value=""/>
<input type="button" value="Show Post Titles" onclick="myajaxfunction()"/>
<div class="showdiv"></div>
</form>
<小时>function rs_like_count() {
if ( isset($_REQUEST) ) { // $_REQUEST is having all the data sent using ajax
$post_type= $_REQUEST[\'post_type\'];
echo $post_type;
}
die();
}
add_action( \'wp_ajax_rs_like_request\', \'rs_like_count\' );