Jquery .ajax()被触发两次

时间:2012-07-17 17:51:37

标签: php jquery ajax wordpress

searchform.php

<div>
    <dl>
        <dd>
            <select name="shr" id="selection" >
                <option value="#All">Select one</option>
                <option value="16">One</option>
                <option value="17">Two</option>
            </select>
        </dd>
    </dl>
</div>

的functions.php:

add_action( 'wp_ajax_nopriv_load-filter', 'ajax_func' );
add_action( 'wp_ajax_load-filter', 'ajax_func' );
function ajax_func () {

$suggestions = array();

$tax = 'tracks';
$args = array ('hide_empty' => 0);
$child_terms = get_terms($tax, $args);
$esen = $_POST['cat'];

foreach ($child_terms as $child_term) :
    $parent_of_child = $child_term->parent;
    $parent_term = get_term_by('term_id', $esen, 'tracks');
    if ($parent_of_child == $esen) {

        $suggestion = array();
        $suggestion['child_term'] = $child_term->name;
        $suggestions[]= $suggestion;

    }
endforeach;

echo $suggestion['child_term'];
exit;
}

Js文件:

var select = $("#selection");
var zia = $("#selection").val();

select.bind("change",
    function(){
        $.ajax({
            type: 'POST',
            url: 'http://localhost/wp/wp-admin/admin-ajax.php',
            data: {'action': 'load-filter', cat: zia },
            success: function(value) {
                alert(zia);
            }
        });
    }
);

我简化了我复杂的jquery函数,但即使这个代码也会被触发两次,即它会两次警告“zia”变量。我删除了add_actions中的一个,看它是否会导致双重触发,但没有运气。任何的想法?

0 个答案:

没有答案