简体   繁体   中英

html form without submit button and anchor link onclick redirect with input value

I want to redirect people like this: example.com/search/[search+text]

html source code:

<form name="menu_search" method="get" onsubmit="window.location.href = 'http://www.col3negmovie.com/search/'+this.q.value+''; return false;" >
    <input name="q" id="videoSearch" />
</form>

this is the anchor link :

<a href="./#" onclick="document.menu_search.submit(); window.location.href='http://www.col3negmovie.com/search/'+this.q.value+''; return false;">Search</a>

onclick doesn't work, how could i fix this?

onclick="document.menu_search.submit();
window.location.href='http://www.col3negmovie.com/search/'+this.q.value+'';
return false;"

Your form:

<form name="menu_search" method="get" onsubmit="window.location.href = 'http://www.col3negmovie.com/search/'+document.getElementsByName('q')[0].value+''; return false;" >
<input name="q" id="videoSearch" />
</form>

Anchor tag:

<a href="./#" onclick="document.getElementsByName('menu_search')[0].submit(); return false;">Search</a>



Or without the <form> tag (which makes it less confusing):

<input name="q" id="videoSearch" />
<a href="./#" onclick="window.location.href = 'http://www.col3negmovie.com/search/' + document.getElementById('videoSearch').value;">Search</a>

To do the same when 'Enter' is pressed:

document.getElementById("videoSearch").onkeyup = function() {
if (this.event.keyCode == 13 || this.event.which == 13)
    //Enter was pressed, handle it here
    window.location.href = 'http://www.col3negmovie.com/search/' + document.getElementById('videoSearch').value;
}
}

Hope that helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM