我在这里读过30到40多个关于它的问题,但没有实现其中的任何一个。我说“不能”是因为我可能错过了什么或者试图让他们在wordpress上工作有一些困难。
“su_button url="#”style=“平面”background=“# Me 2079”size="2“wide=”是“icon=”图标:铅笔“onclick=execJS();点击我
含量faucad.js
function execJS() {
do_shortcode([my_shortcode cmd="take"]);}
add_shortcode(‘my_短路码’,'my_shortcode_array‘);
它接受一个名为"cmd“的属性,它应该是”带“。
它不认识这个功能,不能用wordpress搭建一座桥梁;
do_shortcode(my_shortcode cmd="take");
我怎样才能访问那个短代码?我想要实现的就是在单击按钮时触发一个短代码。
如果问题复杂,请告诉我量身定做。
谢谢,
更新:
如果我将dataType设置为"json“,就会得到"Uncaught:'type‘of null”错误。如果我把它作为"html",我总是会收到“失败”消息,代码不会碰do_shortcode()函数。
JS
function gxfd_claim() {
$(document).ready( function() {
$.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {action: "gxfd_claim_function"},
success: function(response) {
if(response.type == "success") {
alert("Success!")
}
else {
alert("Failure!")
}
}
})
})
}插件的PHP
add_action('wp_ajax_nopriv_gxfd_claim_function', 'gxfd_claim_function');
add_action('wp_ajax_gxfd_claim_function', 'gxfd_claim_function');
function gxfd_claim_function() {
do_shortcode('[gx_faucad cmd="claim"]');
die();
}
add_action( 'init', 'my_script_enqueuer' );
function my_script_enqueuer() {
wp_register_script( "faucad", WP_PLUGIN_URL.'/gx-internal/faucad.js', array('jquery') );
wp_localize_script( 'faucad', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'faucad' );
}发布于 2018-05-08 21:53:54
试着改变
success: function(response) {
if(response.type == "success") {
alert("Success!")
}
else {
alert("Failure!")
}
}至
success: function(response, status) {
alert(status);
}success函数根据http://api.jquery.com/jQuery.ajax传递这些参数
(Anything data, String textStatus, jqXHR jqXHR)可能发生的情况是,PHP脚本不返回数据,因此data (response)参数是null,当您尝试从null参数访问type时,它会抛出TypeError。
https://stackoverflow.com/questions/50234838
复制相似问题