- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 React 和 Javascript 我不确定在哪里添加函数。我的输入工作得很好——但我意识到我需要添加一个函数或能力来接受用户输入的任何内容并将字符串转换为小写格式。我试着把它放在输入字段中,但这只会强制用户输入的任何内容都以小写形式输入,所以它在那里不起作用。
const {useState} = React;
const SearchBar = (props) => {
const [songSearch, setSongSearch, ] = useState("");
function searchResults(event) {
event.preventDefault();
let response = props.songs.filter((song) => {
if (song.album.includes(songSearch) || song.artist.includes(songSearch) || song.title.includes(songSearch)
|| song.genre.includes(songSearch) || song.release_date.includes(songSearch)){
return true;
}
});
console.log(response)
props.setSongs(response);
}
return (
<div >
<form onSubmit={searchResults}>
<div>
<input className="search-bar-input"
type="text"
value={songSearch}
onChange={(e) => setSongSearch(e.target.value)}
placeholder="Search by: Title, Artist, Album, Release Date and Genre"
/>{" "}
<button className="submit-button" type="submit">Search</button>
</div>
</form>
</div>
);
};
const fixedSongs = [...Array(10).keys()]
.map(x => x+1)
.map(id => ({
album: `Album Num ${id}`,
artist: `Artist Num ${id}`,
title: `Title Num ${id}`,
genre: `Genre Num ${id}`,
release_date: `Release Date Num ${id}`
}));
const Thingy = ({...props}) => {
return (
<div>
<SearchBar songs={fixedSongs} setSongs={() => {}}/>
</div>
);
};
ReactDOM.render(
<div>
DEMO
<Thingy />
</div>,
document.getElementById("rd")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script>
<div id="rd" />
最佳答案
const {useState} = React;
const SearchBar = (props) => {
const [songSearch, setSongSearch] = useState("");
function searchResults(event) {
event.preventDefault();
// obtain "lower-case" version of the user-input (songSearch)
const lowerCaseSongSearch = songSearch.toLowerCase();
// avoid listing every column to search by using below array
const searchCols = ['album', 'artist', 'title', 'genre', 'release_date'];
let response = props.songs.filter((song) => {
// find if "songSearch"'s lower-case version exists in any
// of the 5 search-columns (ie, album, artist, etc)
if (
searchCols.some( // if "some" element (album, artist, etc)
k => song[k] // has the lower-case version of "songSearch"
.toLowerCase() // matching the lower-case version of album, artist, etc
.includes(lowerCaseSongSearch)
)
) { // if lower-case matched, return "true"
return true;
}
});
console.log(response)
props.setSongs(response);
}
return (
<div >
<form onSubmit={searchResults}>
<div>
<input className="search-bar-input"
type="text"
value={songSearch}
onChange={(e) => setSongSearch(e.target.value)}
placeholder="Search by: Title, Artist, Album, Release Date and Genre"
/>{" "}
<button className="submit-button" type="submit">Search</button>
</div>
</form>
</div>
);
};
const fixedSongs = [...Array(10).keys()]
.map(x => x+1)
.map(id => ({
album: `Album Num ${id}`,
artist: `Artist Num ${id}`,
title: `Title Num ${id}`,
genre: `Genre Num ${id}`,
release_date: `Release Date Num ${id}`
}));
const Thingy = ({...props}) => {
return (
<div>
<SearchBar songs={fixedSongs} setSongs={() => {}}/>
</div>
);
};
ReactDOM.render(
<div>
DEMO
<Thingy />
</div>,
document.getElementById("rd")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script>
<div id="rd" />
关于javascript - 强制用户输入的字符串变成小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72047279/
我已经阅读了这个答案https://stackoverflow.com/a/45486495/1108891 ,它演示了元组类型推断。经过一些实验,我遇到了这种情况。 当我们的 tuple 函数有 T
我想删除零, 我喜欢这个模型,如果我输入 1000 然后额外的表显示从 1 到 1000 的所有数字,每个数字都会检查并删除零。 示例:如果我输入 10然后输出如 1 2 3 .....8 9 1(1
鉴于我对PowerShell的了解仍在开发中,请与我一起提出任何建议/答案。 因此,在我工作的地方我们工作的公司拥有大量日文机器,需要注册Intune。但是,我们正在运行的脚本无法在其计算机上运行,
我刚刚制作了一个将路径保存到 INI 文件中的小程序。 但是在输出中,路径是这样写的: C:\\Windows 我想这样写: C:\Windows 我用 string.replace 尝试了很多方法,
所以我尝试 std::replace(diff_path.begin(), diff_path.end(), "\\", "/"); 但它似乎无法在我的 Visual Studio 上编译.怎么办 -
我使用以下代码每 30 秒自动抓取/设置最新的页面标题: setInterval(function() { var data = "http://mysite.com/mypage
我希望有两个 View 是组成集的一部分。每个 View 中的数据最好在 UITableView 中表示。然后,我想添加一个手势来使 View 在屏幕上闪烁,并引入另一个类似的 View ,并带有页面
我正在尝试开发一个小游戏,但我遇到了以下问题:我有一个伪类“Cannon”,每个 Cannon 都有一个存储它应该守卫的区域的数组和一个存储“入侵者”的数组进入其中一个戒备区。我创建了下一个函数作为
当我从应用程序中进行插入时,所有 ★(星号)都会变成“â…” 如何阻止这种情况发生? *如果我直接通过 phpmyadmin 插入它,它就可以工作,但使用这个 php 时则不行: connect_er
我遇到了一个奇怪的问题,将 NSDictionary 存储到 NSUserDefaults,然后检索它会将其转换为 NSCFString。 这是我保存数据的地方: - (void)saveProgre
我正在尝试像这样向 coinbase api 发出请求 $url = "https://api.gdax.com/products/BTC-USD/candles?start=".date($form
我在 HTTP header 中使用 if-modified-since 来决定是否应该下载文件。应用程序已经过测试,一切正常,但现在当我询问我的 NSHTTPURLResponse 实例 respo
我向串口发送0xFF 结果是 0x3F。 所有其他字节都是正确的。 情况是这样的…… 外部盒子将这些字节发送到 PC... 0xFF, 0x0D, 0x00, 0x30, 0x31, 0x53, 0x
所以我在我的 Next JS 应用程序中遇到了这个奇怪的问题,我导入了谷歌字体,如下所示 在我的浏览器中显示的不是 href,而是 data-href="...",所以问题是谷歌无法将此识别为链接
我试图通过将 QRect 变成 QPolygon 来检查 QPolygon 和 QRect 之间的碰撞。但是,矩形也可能有我添加的旋转,所以我想知道如何将 QRect 变成 QPolygon 并考虑到
我正在尝试写一个 Conduit使用 attoparsec解析器。具体来说,给定 parseOne :: Parser T , 我想构建一个 Conduit ByteString m T重复地将解析器
标记内的超链接
我正在尝试添加 和 所以实际的文字出现在我的页面上。不是链接。 所以我希望在我的页面上显示实际的 HTML,如下所示: 目前,出现了一个死图像...我想 单独阻止了这一点,只是显示了普通的html?
最近发现一些路由器设备包含后门,some of which可以通过单个UDP数据包加以利用。我意识到其中一些后门不一定是恶意的,因为我在自己的产品中也做了同样的事情以进行故障排除:打开套接字将心跳数据
我知道我可以将 iOS 设备变成 iBeacons ( Can an iOS7 device act as an iBeacon? )。不幸的是,我只有一台设备,我的信标还没有到达。所以我想知道如何将
有没有人尝试过将 MAC 变成 iBeacon。我已经为 iOS 设备完成了此操作,并且想要一个类似的带有一些 UI 的 MAC 独立应用程序。我听说 Mavericks 上的新 API 支持 iBe
我是一名优秀的程序员,十分优秀!