值在onClick事件中的javascript变量没有变化
问题描述:
我想实现一个动态搜索jQuery和PHP,但我无法更改我的onclick事件上的JavaScript变量的值。值在onClick事件中的javascript变量没有变化
<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>
当点击触发
//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;
});
,但是当这之后,我的搜索添加数据仍然搜索老品类
$('#<?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”它改变了价值值不会在“main_url”中改变
答
main_url是一个变量,它只设置一次。更改设置时使用的另一个变量的值不会更改其值。你也应该重置main_url。或者更好的是,使main_url成为返回预期值的函数。