Javascript 如何在加载时运行此form.change()?
我创建了一个脚本,它根据过滤器(用户在表单中选择的过滤器)动态更新查询,并用更新的数据加载外部php文件,但我希望在加载页面时加载更改函数 当用户更改任何输入并加载外部结果时,查询将成功更新。不过,我希望在加载页面时加载结果,而不是等待用户输入 我已经通过存储cookies成功地使表单更新了用户选择的内容,但仍然不知道如何在用户访问页面时加载表单。我试图在$(“.QueryElement”).change(function()的末尾添加.trigger('change'),但结果不一致,无法工作 这是我的代码:Javascript 如何在加载时运行此form.change()?,javascript,jquery,Javascript,Jquery,我创建了一个脚本,它根据过滤器(用户在表单中选择的过滤器)动态更新查询,并用更新的数据加载外部php文件,但我希望在加载页面时加载更改函数 当用户更改任何输入并加载外部结果时,查询将成功更新。不过,我希望在加载页面时加载结果,而不是等待用户输入 我已经通过存储cookies成功地使表单更新了用户选择的内容,但仍然不知道如何在用户访问页面时加载表单。我试图在$(“.QueryElement”).change(function()的末尾添加.trigger('change'),但结果不一致,无法工作
<script type="text/javascript">
$(function() {
/*
* Retrieve cookies in form
*/
$("#showposts").val($.cookie('deluxe-' + 'numposts'));
$(".tags, .categories, #show-all-tags, #show-all-cats").each(function() {
if ($.cookie('deluxe-' + $(this).attr('id')) == 'checked') {
$(this).attr('checked', true);
}
if ($.cookie('deluxe-' + $(this).attr('id')) == 'unchecked') {
$(this).attr('checked', false);
}
});
/*
* This function is to add filters
*/
$(".QueryElement").change(function() {
var query = '';
$.cookie('deluxe-form-state', 'activated', { path: '/', expires: 100 });
query += "showposts=" + $("#showposts").val();
$.cookie('deluxe-' + 'numposts', $("#showposts").val(), { path: '/', expires: 100 });
function add_filters(showall, maincls, type) {
if ($(showall).is(":checked") == false) {
$.cookie('deluxe-' + $(showall).attr('id'), 'unchecked', { path: '/', expires: 100 });
var val = [];
$(maincls + ':checked').each(function(i){
val[i] = $(this).attr('id');
$.cookie('deluxe-' + $(this).attr('id'), 'checked', { path: '/', expires: 100 });
});
$(maincls + ':not(:checked)').each(function(i){
$.cookie('deluxe-' + $(this).attr('id'), 'unchecked', { path: '/', expires: 100 });
});
query += "&" + type + "=" + val;
} else {
$.cookie('deluxe-' + $(showall).attr('id'), 'checked', { path: '/', expires: 100 });
$(maincls + ':checked').each(function(i){
$.cookie('deluxe-' + $(this).attr('id'), 'checked', { path: '/', expires: 100 });
});
$(maincls + ':not(:checked)').each(function(i){
$.cookie('deluxe-' + $(this).attr('id'), 'unchecked', { path: '/', expires: 100 });
});
query += "&" + type + "=";
}
}
add_filters('.show-all-cats', '.categories', 'cat');
add_filters('.show-all-tags', '.tags', 'tag');
$("#result").load('<?php bloginfo('template_directory'); ?>/GetResults.php?' + query);
});
});
</script>
$(函数(){
/*
*检索表单中的cookies
*/
$(“#showposts”).val($.cookie($.cookie('deluxe-'+'numposts'));
$(“.tags、.categories、#显示所有标记、#显示所有猫”)。每个(函数(){
if($.cookie($.cookie('deluxe-'+$(this.attr('id'))=='checked')){
$(this.attr('checked',true);
}
if($.cookie($.cookie('deluxe-'+$(this.attr('id'))=='unchecked'){
$(this.attr('checked',false);
}
});
/*
*此函数用于添加过滤器
*/
$(“.QueryElement”).change(函数(){
var查询=“”;
$.cookie('deluxe-form-state','activated',{path:'/',expires:100});
查询+=“showposts=“++$(“#showposts”).val();
$.cookie('deluxe-'+'numposts',$(“#showposts”).val(),{path:'/',expires:100});
功能添加过滤器(showall、maincls、type){
如果($(showall).is(“:checked”)==false){
$.cookie('deluxe-'+$(showall).attr('id'),'unchecked',{path:'/',expires:100});
var-val=[];
$(maincls+':checked')。每个(函数(i){
val[i]=$(this.attr('id');
$.cookie('deluxe-'+$(this.attr('id'),'checked',{path:'/',expires:100});
});
$(maincls+':未选中)。每个(函数(i){
$.cookie('deluxe-'+$(this.attr('id'),'unchecked',{path:'/',expires:100});
});
查询+=“&”+类型+“=”+val;
}否则{
$.cookie('deluxe-'+$(showall).attr('id'),'checked',{path:'/',expires:100});
$(maincls+':checked')。每个(函数(i){
$.cookie('deluxe-'+$(this.attr('id'),'checked',{path:'/',expires:100});
});
$(maincls+':未选中)。每个(函数(i){
$.cookie('deluxe-'+$(this.attr('id'),'unchecked',{path:'/',expires:100});
});
查询+=”&“+类型+”=”;
}
}
添加_过滤器(“.显示所有类别”、“类别”、“类别”);
添加_过滤器('.show all tags','.tags','tag');
$(“#result”).load('/GetResults.php?'+query);
});
});
如果你需要的话,这是我的查询表
<?php
########################################
# showposts=
########################################
echo '<select id="showposts" class="QueryElement">';
for ($i = 5; $i <= 50; $i += 5) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
echo '</select>';
########################################
# cat=
########################################
$categories = get_categories();
foreach($categories as $cat) {
echo '<input type="checkbox" name="'.$cat->cat_ID.'" id="'.$cat->cat_ID.'" class="categories QueryElement" /> '.$cat->cat_name;
}
echo '<input type="checkbox" name="show-all-cats" id="show-all-cats" class="QueryElement show-all-cats" checked="checked"> Include all categories';
########################################
# tag=
########################################
$tags = get_tags();
foreach($tags as $tag) {
echo '<input type="checkbox" name="'.$tag->slug.'" id="'.$tag->slug.'" class="tags QueryElement" /> '.$tag->name;
}
echo '<input type="checkbox" name="show-all-tags" id="show-all-tags" class="QueryElement show-all-tags" checked="checked"> Include all tags';
?>
编辑:函数末尾的触发器起作用,但仅在首次加载时,当用户在加载页面时更新表单时,我会得到不一致的更改。表单应该充当内容过滤器,外部文件被支持充当表单过滤的内容结果。提取更改函数应该可以做到这一点。
getData();
$('el').change(getData);
function getData(){
//.change function here
}
或
一种新的方法应该有效
$('el').trigger('change');
jquery docs on.trigger()
说明:执行附加到给定事件类型的匹配元素的所有处理程序和行为
好的,好多了,但当我刷新时,我丢失了cookie(表单显示为默认值)。我对以下问题非常困惑:(您尝试了哪种解决方案?如果您使用的是第一种解决方案,上下文可能会出错我尝试了$(“.queryElement”).trigger('change'));在脚本之上,它没有这样做。第一个解决方案可以工作,但Cookie/form值始终重置为默认值。谢谢!我尝试在脚本下面放置$(“.QueryElement”).trigger('change');它工作得非常好!LIfesaver!!谢谢..非常感谢