简体   繁体   中英

Accept enter key in JavaScript inside a text box

I have a script set up where someone picks a comic to go to out of the list by appending it to the URL. However, I can't for the life of me get it to accept an enter stroke while inside the text box and still maintain script functionality. Any solutions would be very very much appreciated.

Script/Form looks like this:

function GoToURL(j) { window.status=('Extracting')

 var URLis; URLis = document.URLframe.Dest.value if (URLis == "" || URLis.length <= 0 || URLis > 20 ) { j.value = "Try Again" alert('Pick a comic that exists.'); window.status=('That comic does not exist.') } else { j.value = "Going" + URLis var location=("aaw" + URLis +".html"); this.location.href = location; window.status=('Connecting to ' + URLis + ' Please wait........'); } } </script> <form name="URLframe"> <b>Comic #:</b> <input type="TEXT" name="Dest" size="5" maxlength="3"> <input type="Button" name="Go to" value="Go" onClick="GoToURL(this)"> </form> 

Two suggestions.

  1. Move your onclick="GoToURL(this)" event handler to the form's onsubmit handler.
  2. Change your button type to type="submit" .

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