在键入时或在输入失去焦点时将输入值保存到变量中
Saving an input value into a variable as you type, or as soon as the input loses focus
我研究这个问题已经有一段时间了,我想到的每个解决方案"几乎都有效"。理想情况下,我希望在输入时将输入值保存到变量中,这样我就不必在将输入值保存到变量之前执行任何命令。因为我无法找到解决方案,所以我一直在努力让它在输入失去焦点时立即改变。我试过。focusout,。change和。blur,但没有一个是在输入失去焦点的那一刻改变它。
//This will save the input value into the variable formInput
var formInput = $("#someForm input[type=text]").val();
//This successfully changes the value of formInput, but only after performing
//another command, such as clicking go or pressing enter.
//Tab does not count to the input "losing focus"
$("#someForm input[type=text]").focusout(function() {
$(this).val() = formInput.val();
});
// Same as focusout, as well as blur.
$("#someForm input[type=text]").change(function() {
$(this).val() = formInput.val();
});
//Nope.
$("#someForm input[type=text]").keyup(function() {
formInput = $("this").val();
});
在现代浏览器中,监听input
事件:
$("#someForm input[type=text]").on("input", function() {
formInput = $(this).val();
});
但是keyup
处理程序也应该工作,如果你清楚你的困惑是否formInput
是一个字符串值包含字段的值或一个jQuery对象链接到字段本身(如果formInput
是一个字符串,它不会有val()
方法)。
还有这个代码:
$("this")
选择所有<this>
元素,如果返回任何东西,我会感到惊讶。
Try
var inputText;
$("#someForm input[type=text]").keyup(function() {
inputText = $(this).val();
});
相关文章:
- 输入框值将保存到 MySQL 数据库
- 如何在视图中保存用户输入内容,并在离开页面时对控制器进行后期调用
- 将表单输入保存到txt,弹出结果,不更改页面
- 离子将输入保存为变量
- 将键盘输入保存到变量中
- 如何将jQuery克隆的文本输入保存到cookie或html5存储中
- PHP/MySQL:将用户输入保存到数据库
- 我正在尝试将javascript文本框的输入保存回服务器.我想从保存的弹出文本框中获得输入,以重命名文件夹
- 如何将提示输入保存到数组中
- 如何将文本输入保存为变量,然后提交到服务器(ajax 和 javascript)
- 将输入保存到 Parse (Javascript) 中
- 使用 HTML/JS 将输入保存到文本文件
- 将多个用户输入保存到本地存储中
- Angularjs将表单输入保存到php和Mysql
- 如何在JavaScript中将用户输入保存到变量中
- 将用户输入保存到段落中
- 如何将用户输入保存到HTML和JavaScript中的变量中
- 如何使用ColdFusion将表单输入保存到会话变量中
- 如何允许浏览器将文本输入保存为密码
- Web文本输入保存到数据库中,无需表单-Django Javascript