无法使用包含 url 变量的 javascript 函数刷新到页面底部

Cant refresh to bottom of page with javascript function containing url variables

本文关键字:刷新 函数 底部 javascript 包含 变量 url      更新时间:2023-09-26

我的php页面底部有一些级联下拉菜单。每次用户从下拉列表中选择一个选项时,都会调用以下函数以将该选项的值添加到我的 url 变量中。目前,页面每次都会刷新到顶部,这是一个巨大的问题。通常我会使用 onCLick="window.location='page.htm#bottom';" 之类的东西刷新到页面底部,但当我添加 #bottom 时,下面的函数停止工作。有人可以帮我调整此功能或给我其他想法,这些想法将在功能完成后刷新到页面底部。

function reload5(form){
if(document.getElementById('fda1').checked) {
 var fda = '1';
}else if(document.getElementById('fda0').checked) {
  var fda = '0';
}
var val=form.category.options[form.category.options.selectedIndex].value; 
var val2=form.subcat.options[form.subcat.options.selectedIndex].value;
var val3=form.subcat1.options[form.subcat1.options.selectedIndex].value;
var val4=form.subcat2.options[form.subcat2.options.selectedIndex].value;
var comp1=form.mname.options[form.mname.options.selectedIndex].text;
var itemnum=document.getElementById('item').value; 
var desc=document.getElementById('desc').value;
var quan=document.getElementById('quan').value;
var list=document.getElementById('list').value;
var uom=form.uom.options[form.uom.options.selectedIndex].text;
self.location='add_products.php#bottom?fda=' + fda + '&desc=' + desc + '&quan=' + quan + '&list=' + list + '&uom=' + uom + '&item=' + itemnum + '&cat=' + val + '&cat2=' + val2 + '&cat3=' + val3 + '&cat4=' + val4 + '&comp=' + comp1 ;
}

所以这行不通:self.location='add_products.php#bottom?fda=' + fda

但这确实是:self.location='add_products.php?fda=' + fda

知道把 #bottom 放在哪里吗?

问题是哈希必须在末尾的查询字符串之后。 请参阅此文章。 试试这个...(我也清理了代码)

function reload5(form){
    var val=form.category.options[form.category.options.selectedIndex].value,
        val2=form.subcat.options[form.subcat.options.selectedIndex].value,
        val3=form.subcat1.options[form.subcat1.options.selectedIndex].value,
        val4=form.subcat2.options[form.subcat2.options.selectedIndex].value,
        comp1=form.mname.options[form.mname.options.selectedIndex].text,
        itemnum=document.getElementById('item').value,
        desc=document.getElementById('desc').value,
        quan=document.getElementById('quan').value,
        list=document.getElementById('list').value,
        uom=form.uom.options[form.uom.options.selectedIndex].text,
        fda;
    if(document.getElementById('fda1').checked) {
         fda = 1;
    } else if(document.getElementById('fda0').checked) {
         fda = 0;
    } else {
         fda = -1;
    }
    window.self.location.href = 'add_products.php?fda=' + fda + '&desc=' + desc + '&quan=' + quan + '&list=' + list + '&uom=' + uom + '&item=' + itemnum + '&cat=' + val + '&cat2=' + val2 + '&cat3=' + val3 + '&cat4=' + val4 + '&comp=' + comp1 + "#bottom";
}

我不明白的是,为什么你不使用Ajax做一些根本不会导致页面刷新的事情。