最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onclick window.location.href with input value - Stack Overflow

programmeradmin4浏览0评论

I have an input and I want the user to be sent to another page with the input's value in the URI so that I can extract it.

This is how my input is set up:

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

Basically, when the date is changed, the date should be added onto the end of the URI and the user should be sent to test.php, where the value will be extracted. However, the value doesn't seem to be added on.

What's the problem?

I have an input and I want the user to be sent to another page with the input's value in the URI so that I can extract it.

This is how my input is set up:

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

Basically, when the date is changed, the date should be added onto the end of the URI and the user should be sent to test.php, where the value will be extracted. However, the value doesn't seem to be added on.

What's the problem?

Share Improve this question asked Feb 3, 2014 at 22:47 user2397282user2397282 3,81815 gold badges49 silver badges98 bronze badges 1
  • you need to use single quotes. – putvande Commented Feb 3, 2014 at 22:48
Add a comment  | 

2 Answers 2

Reset to default 12

Try:

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

You don't need to do document.getElementById('datepicker') since the element you are in is already the one you want the value from.

The problem is that you're using double-quotes to delimit the attribute value and the string value within the onchange event. Try using single-quotes, like this:

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

And of course, since you've tagged the question with jQuery, why not use it?

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

...

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

评论列表(0)

  1. 暂无评论