CS50 Web编程项目0:无JS实现Google高级搜索表单遇跳转问题求助
Got it, let's sort out why your advanced search form is just bouncing back to the Google Advanced Search page instead of returning results. The issue comes down to how you've set up the form's action attribute—let's break this down and fix it.
The Root Problem
Your current form action is pointing to the Google Advanced Search form page (with hardcoded placeholder parameters like as_q=q), not the endpoint that actually processes search requests. When you submit the form, it's just loading that form page again instead of sending your input to Google's search engine. Also, those hardcoded parameter values would override any user input anyway!
Corrected Code
Here's the fixed version of your form—small changes, but they make all the difference:
<form action="https://www.google.com/search" method="get"> <div>所有字词:<input type="text" class="input_advanced" name="as_q"></div> <div>精确字词或短语:<input type="text" class="input_advanced" name="as_epq"></div> <div>任意字词:<input type="text" class="input_advanced" name="as_oq"></div> <div>排除字词:<input type="text" class="input_advanced" name="as_eq"></div> <input type="submit" id="Search_button" value="Advanced Search"> </form>
Key Changes Explained
- Updated
actionURL: We're now pointing tohttps://www.google.com/search—this is Google's core search endpoint that processes all search queries, including advanced ones. - Added
method="get": While forms default to GET, explicitly defining it makes the behavior clear. Google uses GET requests to pass search parameters in the URL, which is exactly what we want. - Removed hardcoded parameters: Your original action had
as_q=qand others—these were forcing the parameter values to be literal strings like "q" instead of the user's input. By removing them, the form will automatically append each input'sname(which you already got right:as_q,as_epq, etc.) and the user's typed value to the search URL.
How It Works Now
When you fill in the fields and submit, the browser will generate a URL like this (example values included):https://www.google.com/search?as_q=CS50+Python&as_epq=web+programming&as_oq=JavaScript+HTML&as_eq=jQuery
This tells Google to run the advanced search with your input, and you'll get the matching results page instead of looping back to the form.
内容的提问来源于stack exchange,提问作者Saed Mohamed

