最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Call jQuery function from PHP?

matteradmin13PV0评论

Right now I have code that looks like this:

function search_ajax_fetch() {
?>
<script type="text/javascript">
  function searchFetch() {
    var keyword = $('#keyword').val();
    if ( keyword ) {
    $('#datafetch').show();
    $.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: {
          action: 'data_fetch',
          keyword: keyword
        },
        success: function(data) {
            $('#datafetch').html( data );
        }
    });
    } else {
      $('#datafetch').html( '' );
      $('#datafetch').hide();
    }
  }
</script>
<?php
}
add_action( 'wp_footer', 'search_ajax_fetch' );

But I am trying to move all the jQuery to an external file, and call this function instead. But I cannot make it work by moving it to an external file and doing it like this:

function search_ajax_fetch() {
?>
<script type="text/javascript">
  searchFetch();
</script>
<?php
}
add_action( 'wp_footer', 'search_ajax_fetch' );

Why isn't this working? The js file is loaded correctly!

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far