当前位置: 首页 > news >正文

首页搜索框传递参数,并在搜索页面中的搜索框中进行显示,搜索框绑定回车键进行搜索

实现搜索条件和搜索内容固定,以及回车键搜索跳转

1.写出搜索条件和搜索框

<form class="parent"><select id="searchSelect"  style="border: 1px solid #325da7;border-right: none;" value="resource"><option value="resource">资源名称</option><option value="user">作者名称</option></select><input type="text" class="search" placeholder="搜索" id="searchtxt" /><input type="button" id="selectBtn" class="btn" />
</form>

2.添加scss样式

.parent {margin-top: 10px;display: flex;flex-wrap: nowrap;.search {width: 448px;height: 36px;outline: none;border: 1.5px solid #325da7;}.btn {height: 40px;width: 60px;background: #325da7 url('https://www.ydwenku.com/master/images/searchico.png') center center no-repeat;background-size: 35px;border: none;}}

页面
在这里插入图片描述

3.搜索框选择条件,在搜索框中输入内容,然后进行页面跳转

let selectedValue = 'resource';
const searchSelectElement = document.getElementById("searchSelect");  //搜索框条件选择下拉框
const searchInputElement = document.getElementById("searchtxt"); //搜索输入框
const selectBtnElement = document.getElementById("selectBtn"); //搜索按钮
searchSelectElement.addEventListener("change", searchSelect);
searchInputElement.addEventListener("input", handleSearchInput);
searchInputElement.addEventListener("keypress", handleSearchKeyPress);function handleSearchKeyPress(event) {if (event.key === "Enter") {event.preventDefault(); // 阻止默认行为openSearchPage(searchInputElement.value);}
}function handleSearchInput(event) {const searchText = event.target.value;selectBtnElement.onclick = function () {openSearchPage(searchText);};
}function openSearchPage(searchText) {const url = `/html/searchpage/search.html?searchText=${searchText}&selectSearch=${selectedValue}`;window.open(url);
}

在搜索框中输入内容,然后进行页面跳转
在这里插入图片描述

4.页面跳转后从地址栏中获取相关数据,搜索条件和搜索框中的内容

const urlsearch = new URLSearchParams(location.search);
const serchname = urlsearch.get("searchText");  //获取地址栏中传递的参数
const serChange = urlsearch.get("selectSearch");
$('#searchtxt').val(serchname);
$('#searchSelect').val(serChange)

在这里插入图片描述
从地址栏中获取到相关数据searchText和selectSearch的值

$('#searchtxt').val(serchname);  // 搜索框中条件固定
$('#searchSelect').val(serChange) // 搜索框中搜索内容固定

在这里插入图片描述

http://www.lryc.cn/news/146068.html

相关文章:

  • 【Vue3+Ts】项目启动准备和配置项目代码规范和css样式的重置
  • Java【手撕双指针】LeetCode 15. “三数之和“, 图文详解思路分析 + 代码
  • Flutter:自定义组件的上下左右弹出层
  • C++处理终端程序中断或意外退出的情况
  • 分布式锁:业务锁和定时任务锁
  • 路由器的简单概述(详细理解+实例精讲)
  • Mapper.xml文件解析
  • ES 7.6 - JAVA应用基础操作篇
  • com.squareup.okhttp3:okhttp 组件安全漏洞及健康度分析
  • 【Unity的HDRP渲染管线下用Steam VR串流结合使用遇到的各种问题_SteamVR 插件和Pico串流助手】
  • Unity——音乐、音效
  • Ubuntu 23.10 将首次推出基于 Flutter 的新 Ubuntu 商店
  • linux scatterlist阅读三
  • 2023新,centos7安装mysql8.0.25
  • Data Rescue Professional for Mac:专业的数据恢复工具
  • 新手小白想要做好跨境电商独立站,需要考虑哪些要素?
  • Consul原理介绍
  • 【C++实战】C++实现贪吃蛇(含源代码)—基于easyx图形库
  • PHP获取两个日期之间的所有日期
  • STL之stack(适配器讲解以及双端队列的讲解)
  • JVM解密: 解构类加载与GC垃圾回收机制
  • 【Spring Boot】Spring Boot结合MyBatis简单实现学生信息管理模块
  • 【Java List与Map】List<T> Map与Map List<T>的区别(126)
  • 【FreeRTOS】常用函数总结
  • The Cherno——OpenGL
  • linux中学习控制进程的要点
  • C++Qt QSS要注意的坑
  • LeetCode每日一题:56. 合并区间(2023.8.27 C++)
  • 电视盒子什么牌子好?经销商整理线下热销电视盒子品牌排行榜
  • JavaScript关于函数的小挑战