【问题标题】:onclick window.location.href with input value带有输入值的 onclick window.location.href
【发布时间】:2014-02-03 22:47:07
【问题描述】:

我有一个输入,我希望将用户发送到另一个页面,并将输入的值包含在 URI 中,以便我可以提取它。

我的输入是这样设置的:

<input name="date" id="datepicker" onchange="window.location.href = 'test.php?Date=' + document.getElementById("datepicker").value;">

基本上,当日期更改时,应将日期添加到 URI 的末尾,并将用户发送到 test.php,其中将提取值。但是,该值似乎没有增加。

有什么问题?

【问题讨论】:

  • 你需要使用单引号。

标签: javascript php jquery onchange window.location


【解决方案1】:

试试:

<input name="date" id="datepicker" onchange="window.location.href = 'test.php?Date=' + this.value;">

您无需执行document.getElementById('datepicker'),因为您所在的元素已经是您想要从中获取值的元素。

【讨论】:

  • 如果您对解决方案感到满意,请不要忘记接受答案。
  • 我会的,我只需要等 10 分钟,因为你太快了!
【解决方案2】:

问题在于您使用双引号来分隔onchange 事件中的属性值字符串值。尝试使用单引号,如下所示:

<input name="date" id="datepicker" onchange="window.location.href = 'test.php?Date=' + document.getElementById('datepicker').value;">

当然,既然你已经用 jQuery 标记了问题,为什么不使用它呢?

<input name="date" id="datepicker">

...

$("#datepicker").change(function() { 
    window.location.href = 'test.php?Date=' + this.value;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    相关资源
    最近更新 更多