我想在jquery中添加php代码。我知道该文件不能保存为js文件,但必须保存为php文件才能执行php代码。如果我们将其保存为php文件,那么它也不能在函数中排队。php。
jQuery(document).ready(function() {
jQuery(\'#a, #b\').change(function () {
var $this = jQuery(this), // Get a handle on the checkbox we just clicked.
$a = jQuery(\'#a\'),
$b = jQuery(\'#b\'),
$c = jQuery(\'#c\'), // Get a handle on the textbox.
$d = jQuery(\'#d\'), // Get a handle on the textbox.
$e = jQuery(\'#e\'), // Get a handle on the textbox.
$f = jQuery(\'#f\'), // Get a handle on one of our default values.
$g = jQuery(\'#g\'); // Get a handle on one of our default values.
if ($this.attr(\'id\') == \'a\' && $this.is(\':checked\')) {
// Clicking checkbox a will add the content of c and f and place it in e
// It will also uncheck checkbox b.
$e.val( <?php echo stripslashes(get_option(\'m\')); ?> );
$b.removeAttr(\'checked\');
} else if ($this.attr(\'id\') == \'b\' && $this.is(\':checked\')) {
// Clicking checkbox b will add the content of d and g and place it in e
// It will also uncheck checkbox a.
$e.val( <?php echo stripslashes(get_option(\'m\')); ?> );
$a.removeAttr(\'checked\');
} else {
$e.val(\'\');
}
});
});
那么,如何在jquery中添加php并将其集成到函数中呢。php主题文件。还请注意,php echo将返回一个javascript代码,因此该代码可能只是作为文本添加,而不是作为已执行代码进行echo。请帮忙。
SO网友:Joseph Leedy
我认为您没有理由在JavaScript文件中使用PHP。使用wp_enqueue_scripts
挂钩,然后使用wp_localize_script()
函数将数据传递给JavaScript代码。
<?php
function wpa67325_enqueue_scripts() {
wp_enqueue_script( \'some_handle\', \'filename.js\', false );
$translation_array = array( \'some_string\' => __( \'Some string to translate\' ), \'a_value\' => \'10\' );
wp_localize_script( \'some_handle\', \'object_name\', $translation_array );
}
add_action( \'wp_enqueue_scripts\', \'wpa67325_enqueue_scripts\', 10 );