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

时间:2012-04-08 15:24:47

标签: javascript

我的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?

1 个答案:

答案 0 :(得分:1)

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

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做一些不会导致页面刷新的原因。