jquery - How to pass user input to jquery .click using eventdata?
问题描述
Have two buttons which use jQuery's .click
to activate a function. The function is passed an integer parameter so currently one button passes 3
and one passes 4
to the function sort()
. Ideally I would like the user to input whatever integer they like and then have only one button that uses the .click
. The user input could be a drop down selector or manually input. This sounds like a form to me but I don't know how to bring a form and .click
together. I'm still a beginner on this, so maybe this is a fairly simple GET form that can work with the .click
?
My current code includes:
<button class="btn btn-warning" type="submit" id="freeze3">» Random Groups 3</button>
<button class="btn btn-warning" type="submit" id="freeze4">» Random Groups 4</button>
$( "#freeze3" ).click(function() {
if (moving == true) {
clearInterval(interval);
setTimeout(sort(3), 1000);
}
moving = false;
});
$( "#freeze4" ).click(function() {
if (moving == true) {
clearInterval(interval);
setTimeout(sort(4), 1000);
}
moving = false;
});
解决方案
您可以使用基本的常规文本输入、选择或数字输入。使用纯文本输入,您可以执行以下操作,this.value
用于获取用户输入的输入:
$( "#myInput" ).change(function() {
if (moving == true) {
clearInterval(interval);
setTimeout(sort( this.value ), 1000);
}
moving = false;
});
当然,您还需要进行一些基本验证,以确保他们输入的内容有效。另请注意,我正在使用更改事件,但您也可以使用模糊。或者你可以有一个文本输入和一个按钮,使用按钮在点击时触发功能。
推荐阅读
- visual-studio - 如何阻止 NuGet 打包包含不需要的文件
- java - 在触摸外部时关闭弹出窗口
- sql - 如何使用 Laravel Eloquent 在选择中添加一列
- c# - CrossPlatform Xamarin 表单:如何在本地存储中创建 PDF 文件
- python - Python 程序在第 400 次循环后被杀死
- python-3.x - 如何从 django-filter 的 generic.ListAPIView 类中获取请求对象?
- python - 如何优雅地一次绘制多个散点图
- windows - WinSCP:SFTP 脚本传输然后将本地文件移动到另一个文件夹
- jenkins - 手动触发的管道作业可以接受用户输入的参数吗?
- apache - 将 https 非 www 重定向到 www