1

変数の定義:

// Define Autocomplete Location Variable
var ac_location;

変数を設定:

// Set Autocomplete Location Variable Equal to input id "loc"
$("#loc").val(ac_location);

印刷変数:

// Print Autocomplete Variable
alert(ac_location);

比較変数:

// Compare "#loc" input with ac_location variable
if (ac_location.val() != $("#loc")) {
// Print Autocomplete Variable
alert(ac_location);
} else {
alert("No match");
}

これを行うために、jquery ヘルプと google からの情報を使用しようとしました。これは完全なコードです:

http://jsfiddle.net/NY3nG/2/

コンソールは私の値が未定義であると言い、アラートボックスに変数を出力しないため、私のステップの何が問題になっていますか。どんな助けでも大歓迎です。

4

3 に答える 3

2
// Set Autocomplete Location Variable Equal to input id "loc"
$("#loc").val(ac_location);

要素の値を に設定するだけで#locac_locationその逆ではありません。行う

ac_location = $("#loc").val();

if正しく書いてください、あなたのjqueryオブジェクトはそうではあり#locませんac_location

if ($("#loc").val() != ac_location)

さらに、比較後、ステートメントが一致alert("No match");する場合。if

于 2012-11-12T13:35:55.280 に答える
1

ac_location の値が未定義の場合、val 関数はその値を設定しません。これは、未定義が入力変数を与えていないかのように扱われるためです -> jQuery は getter 呼び出しと見なします

于 2012-11-12T13:36:24.467 に答える
1

私はあなたのコードを修正しています:

変数の定義:

// Define Autocomplete Location Variable
// This is not a jquery object, just an ordinary js variable.
var ac_location;

変数を設定:

// Set Autocomplete Location Variable Equal to input id "loc"
// You want to take the value of #loc to ac_location
ac_location = $("#loc").val();

印刷変数:

// Print Autocomplete Variable
alert(ac_location);

比較変数:

// Compare "#loc" input with ac_location variable
// ac_location is a string, compare it with value of #loc field:
if (ac_location != $("#loc").val()) {
    // Print Autocomplete Variable
    alert(ac_location);
} else {
    alert("No match");
}
于 2012-11-12T13:40:03.150 に答える