【问题标题】:Adding multiple variables in the page URL using javascript使用 javascript 在页面 URL 中添加多个变量
【发布时间】:2013-05-30 22:37:19
【问题描述】:

由于我对 php 和 javascript 的使用比较陌生,所以在尝试在 URL 中添加多个变量时遇到了这个问题

我使用以下脚本:

<script>
function refresh() {
 var PCWDE = document.getElementById("PC");
 var NMWDE = document.getElementById("naam");
 var VNMWDE = document.getElementById("voornaam");
 var ONDWDE = document.getElementById("onderneming");
 var BTWWDE = document.getElementById("btwnummer");
 var LNDWDE = document.getElementById("land");


        this.document.location.href = "http://example.com/form.php?PCS=" + PCWDE.value "&nm=" + NMWDE.value "&vnm=" + VNMWDE.value "&ond=" + ONDWDE.value "&btw=" + BTWWDE.value "&lnd=" + LNDWDE.value;

}
</script>    

这是通过以下 html 代码激活的:

    <?php
    $pc = $_GET['PCS'];
    echo '<input type="text" name="pc" id="PC" onblur="refresh()" value="'.$pc.'">'
    ?>

使用这段 html 代码的主要原因是我需要在不提交表单的情况下执行查询,同时即使页面刷新时仍然能够保存文本框的值。以上html代码多次使用,使用不同的ID。

我在尝试执行此操作时遇到的问题是,我的代码仅在仅向 URL 添加 1 个变量时才有效,如下所示:

 this.document.location.href = "http://example.com/form.php?PCS=" + PCWDE.value;

否则它不起作用。 'onblur' 事件无法工作,或者脚本无法运行。

有没有办法以类似于我现在正在做的方式将多个变量添加到 URL?

【问题讨论】:

    标签: php javascript url variables


    【解决方案1】:

    你忘了加号。这是一个语法错误,如果您打开错误控制台,您应该会看到错误消息。 (在 Firefox 上你按 control-shift-j)

    应该是:

    this.document.location.href = "http://example.com/form.php?PCS=" + PCWDE.value + "&nm=" + NMWDE.value + "&vnm=" + VNMWDE.value + "&ond=" + ONDWDE.value + "&btw=" + BTWWDE.value + "&lnd=" + LNDWDE.value;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 2016-09-10
      • 2012-11-20
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      • 2014-04-22
      相关资源
      最近更新 更多