First, we use the $(document).ready() method to ensure that the subsequent code is executed only after the document has been fully loaded.
Then, we select the form element with the class name 'sform' and register a submit event listener.
In the `submit` event handler, we first use the `e.preventDefault()` method to prevent the form's default submission behavior, thereby avoiding redirection to the Baidu search page.
Then, we retrieve the keywords entered by the user into the input field.
Next, we will concatenate the value from the input field with "site:www.4s5.cn" to form a complete search query string. This search query string will then be encoded and appended to the Baidu Search URL.
Finally, we use the `window.open()` method to open the concatenated search URL and perform the search on the new page.
htmlpart:
<form name="search" method="get" class="sform" action="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu">
<input class="sinput" name="wd" type="text" placeholder="请输入搜索词">
<button>
<i class="fa fa-search"></i>
</button>
</form>jQuery section:
$(document).ready(function(){
$('.sform').submit(function(e){
e.preventDefault(); // 阻止表单默认提交行为
var keyword = $('.sinput').val();
var searchQuery = 'site:www.4s5.cn ' + keyword;
var searchUrl = 'https://www.baidu.com/s?wd=' + encodeURIComponent(searchQuery);
window.open(searchUrl, '_blank');
});
});