Previously:
I used a form in a project code to send a request to Baidu,
But I ran into a problem. When I clicked search, it still redirected me to the Baidu interface when there was nothing written in the input box. The purpose of this article is: how to realize when the user did not input content but click on the search for a warning and do not jump?
Artillery preparation:
Action: Control which page a request is sent to. Onsubmit: This event is triggered when the form is submitted. Q: How do I get the form submitted? A: The following two conditions cause the browser to automatically trigger the onSubmit event. 1: When you have a form tag that contains a button and you click on it, 2: or an input whose type is Submit and you press Enter after typing,
There are two cases of onSubmit in the form tag:
onsubmit="return false"
Copy the code
onsubmit="return true"
Copy the code
Return returns the following Boolean value back up to the browser. The browser blocks the form submission if onSubmit returns false. Conversely, if true is received, the form is submitted to the page pointed to by the Action attribute.
Start the attack (specific solution)
1: Complete the definition of JS code functions:
In this function, searchInput is the ID name that I defined for the input field tag. And the logic of this function is, when WE submit the form, check to see if there’s anything in my input field, okay? If not, return false, otherwise return true.
2: How does this function work?
Append the code indicated by the arrow to the form tag and return the function return value defined in the previous step. Why do you add return? Please read the explanation of the artillery preparation stage
If this doesn’t work, you might want to try writing the script directly to HTML instead of js.