简体   繁体   中英

Value is not changing in the javascript variable on the onClick event

I am trying to implement a dynamic search jquery and php, but I am not able to change the value of a javascript variable on my onclick event.

<script type="text/javascript">
  var main_category = '<?php echo $main_category?>';
  var main_url = '<?php echo $url?>get/' + encodeURIComponent(main_category) + '/';
  var container_id = '<?php echo $unique_id?>content_area';

    //name
    $('#<?php echo $unique_id?>search_by_name').autocomplete('<?php echo $url?>get_autocomplete_data/' + encodeURIComponent(main_category) + '/names/', {
      matchContains: "word",
      autoFill: true,
      width: 310
    });$('#<?php echo $unique_id?>search_by_name').result(function (event, data){
      var url = main_url + 'search/title/' + encodeURIComponent(data);
      load_div(container_id, url);
    });


    //on click of displayed categories
    $('[rel="<?php echo $unique_id?>sub_category"]').click(function (){
      window['main_category'] = this.title;
      $('#<?php echo $unique_id?>category').html('' +main_category+ '');
      return false;
    });
  });
</script>

It changes the value when on click is fired

//on click of displayed categories
        $('[rel="<?php echo $unique_id?>sub_category"]').click(function (){
          window['main_category'] = this.title;
          $('#<?php echo $unique_id?>category').html('' +main_category+ '');
          return false;
        });

but when after that i add data for the search it still searches for the old category

$('#<?php echo $unique_id?>search_by_name').result(function (event, data){
          var url = main_url + 'search/title/' + encodeURIComponent(data);
          load_div(container_id, url);
        });

"main_category" value is not changing in the "main_url"

main_url is a variable, and it is only set once. Changing the value of another variable used when setting it does not change its value. You should reset main_url as well. Or better yet, make main_url a function that returns the intended value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM