<script type="text/javascript">
window.onload = init;
function init() { //wait for load and watch for click
var button = document.getElementById("searchbutton");
button.onclick = handleButtonClick;
}
function handleButtonClick(e) { //get user input and go to a new url
var textinput = document.getElementById("searchinput");
var searchterm = textinput.value;
window.location.assign("http://google.com/example/" + searchterm)
}
</script>
<form>
<input type="text" name="search" id="searchinput">
</form>
<input type="submit" value="Ara" id="searchbutton">
이 코드 블록에서 사용자 입력을 가져 와서 사용자 입력으로 새 URL로 이동합니다.JS가 양식 태그 사이에있는 경우 JS가 실행되지 않는 이유는 무엇입니까?
마지막 줄을 양식 요소로 옮기면 작동하지 않습니다. 하지만 id를 사용하여 요소를 찾습니다.
제출 버튼이 양식의 일부인 경우 양식 제출에 도움이되며 페이지가 새로 고쳐집니다. – Dev