0

私はこの問題にしばらく取り組んできましたが、私が思いついたすべての解決策は「ほぼ機能します」。理想的には、入力時に入力値を変数に保存したいので、入力値が変数に保存される前にコマンドを実行する必要はありません。その解決策が見つからなかったので、入力がフォーカスを失ったらすぐに変更するように取り組んできました。.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();
});
4

2 に答える 2

0

試す

var inputText;
$("#someForm input[type=text]").keyup(function() {
    inputText = $(this).val();
});
于 2013-03-25T15:07:01.573 に答える
0

最新のブラウザでは、イベントを聞いてくださいinput

$("#someForm input[type=text]").on("input", function() {    
    formInput = $(this).val();
});

ただし、フィールドの値を含む文字列値なのか、フィールド自体にリンクされたjQueryオブジェクトなのか(文字列の場合はメソッドがない)keyupについての混乱を解消した場合にも、ハンドラーは機能するはずです。formInputformInputval()

また、このコード:

$("this")

すべての<this>要素を選択しているので、それが何かを返すとしたら驚きます。

于 2013-03-25T15:18:19.867 に答える