【发布时间】:2013-03-15 02:24:20
【问题描述】:
我已经研究这个问题一段时间了,我提出的每个解决方案“几乎都有效”。理想情况下,我想在键入时将输入值保存到变量中,因此在将输入值保存到变量之前我不必执行任何命令。由于我找不到解决方案,所以我一直在努力在输入失去焦点后立即对其进行更改。我试过 .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();
});
【问题讨论】:
-
你想要
$(this).val(formInput.val());吗?但这会替换您刚刚输入的内容。 -
我真的没有达到你的目标。您正在尝试自行替换输入的值?
-
@dystroy 我正在尝试将输入的值保存到变量 formInput 中,无论是在输入时(动态保存)还是在光标不再位于输入字段中时(由于某种原因,这似乎不是 focusout 的工作方式。)后来,输入中的确切文本被放置到 URL 作为搜索词。问题是变量没有被保存,直到我在中间执行某种命令,例如按表单附带的“Go”按钮,或者在输入后按 Enter。
标签: javascript input var