Hongmu Notes
Home Language Notes Implement Baidu In-site Search Code Using jQuery on a Website
Language Notes PHP

Implement Baidu In-site Search Code Using jQuery on a Website

Implement Baidu In-site Search Code Using jQuery on a Website

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');
  });
});

 

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 143
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 191
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

(Adaptive Mobile Version) Responsive tech product sensor-based website – pbootCMS template; HTML5 blue smart electronic products website source code download – 0159

(Adaptive Mobile Version) Responsive tech product sensor-based website – pbootCMS template; HTML5 blue smart electronic products website source code download – 0159 Practical Collection pbootcms Template

This collection of adaptive mobile-friendly responsive tech products and sensor-themed PbootCMS templates features an HTML5 blue design style. The minimalist, tech-oriented design is ideal for showcasing high-tech products such as sensors and smart hardware. It helps technology R&D enterprises present their innovative products online, attracting investment and partners. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 56
Fashion & Apparel Franchise Display Industry Website Template – 0988

Fashion & Apparel Franchise Display Industry Website Template – 0988 Practical Collection Yiyou template

An EyouCMS website template designed for the apparel and footwear franchise display industry. Its stylish and professional design enables effective presentation of clothing and footwear products, franchise policies, brand image, and success stories, helping apparel brands attract franchisees and agents online. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (...
👁 35
Python error: ModuleNotFoundError: No module named 'fcntl'.

Python error: ModuleNotFoundError: No module named 'fcntl'. Language Notes python

This error indicates that you need the `fcntl` module, but it could not be found. This issue typically occurs on Windows platforms, as `fcntl` is a system call native to Unix-like systems. Therefore, if your code uses the `fcntl` module, you should verify whether your code was written or executed in a Unix environment. If you intend to use this module on a Windows platform...
👁 1296
Agricultural Machinery Production Equipment Website Template 0303

Agricultural Machinery Production Equipment Website Template 0303 Practical Collection Yiyou template

This EyouCMS template is ideal for the agricultural machinery and production equipment industry. Its natural and practical design effectively showcases agricultural machinery products, field operation applications, technical specifications, and brand strength. It enables agricultural machinery manufacturers to showcase their products online and attract agricultural cooperatives and farm owners. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions About EyouCMS Installation...
👁 53
(Adaptive Mobile Version) Atmospheric personal blog – PBootCMS website template; Download source code for a blog theme showcase website – 0814

(Adaptive Mobile Version) Atmospheric personal blog – PBootCMS website template; Download source code for a blog theme showcase website – 0814 Practical Collection pbootcms Template

A professional PbootCMS website template designed for personal blogs and artwork showcases, supporting mobile access. Featuring a modern artistic design style, it is ideal for designers, artists, and bloggers to display their personal works and ideas. This template helps creative professionals build their online personal brand and portfolio. Template Overview | Installation Instructions | Website Admin Panel: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 49
(Adaptive Mobile Version) Responsive bilingual (Simplified/Traditional Chinese) Black Bearing Gear – Mechanical Manufacturing Enterprise PBootCMS Template; Download Source Code for Mechanical Gear Equipment Websites – 0160

(Adaptive Mobile Version) Responsive bilingual (Simplified/Traditional Chinese) Black Bearing Gear – Mechanical Manufacturing Enterprise PBootCMS Template; Download Source Code for Mechanical Gear Equipment Websites – 0160 Practical Collection pbootcms Template

An adaptive, mobile-friendly, responsive bilingual (Simplified and Traditional Chinese) black bearing gear mechanical manufacturing PbootCMS corporate template. Supports both Simplified and Traditional Chinese; features a rugged, professional design style ideal for showcasing precision mechanical components such as gears and bearings. This template helps mechanical manufacturing enterprises conduct brand promotion and product display across mainland China, Hong Kong, Macau, and overseas Chinese markets. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin
👁 51