1

以下のコードでは、3つの異なるイベント(フォーカス、選択、変更)で同じ機能があります。これは冗長に思えますが、3つを1つに組み合わせる方法がわかりません。アイデアをよろしくお願いします!

$("input[name^='addSchool']").autocomplete({
    source: function (request, response) {
        var temp = $("input[name^='addSchool']").attr("name");
        var tempteacherID = temp.split(":");
        var teacherID;
        teacherID = tempteacherID[1]
        $.ajax({
            url: "JSONschools.asp",
            dataType: "json",
            data: { term: request.term, teacherID: teacherID },
            success: function (data) {
                response(data);
            }
        });
    },
    focus: function (event, ui) {
        //if the value of the textbox does not match a suggestion, clear its value
        if (!ui.item) {
            $(this).val('');
            $(this).parent("li").next().html("Please select only from the list shown").effect("pulsate", { times: 3 }, "slow");
        }
        else {
            $(this).next().val(ui.item.id);
        }
    },
    select: function (event, ui) {
        //if the value of the textbox does not match a suggestion, clear its value
        if (!ui.item) {
            $(this).val('');
            $(this).parent("li").next().html("Please select only from the list shown").effect("pulsate", { times: 3 }, "slow");
        }
        else {
            $(this).next().val(ui.item.id);
        }
    },
    change: function (event, ui) {
        //if the value of the textbox does not match a suggestion, clear its value
        if (!ui.item) {
            $(this).val('');
            $(this).parent("li").next().html("Please select only from the list shown").effect("pulsate", { times: 3 }, "slow");
        }
        else {
            $(this).next().val(ui.item.id);
        }
    },
    minLength: 2
});
4

3 に答える 3

3

次のように、名前付き関数として一度宣言できます。

function CheckSuggestion(event, ui) {
  //if the value of the textbox does not match a suggestion, clear its value
  if (!ui.item) {
      $(this).val('');
      $(this).parent("li").next().html("Please select only from the list shown")
                          .effect("pulsate", { times: 3 }, "slow");
  }
  else {
      $(this).next().val(ui.item.id);
  }
}

次に、次のように、匿名関数の代わりにその関数を参照します。

$("input[name^='addSchool']").autocomplete({
    source: function (request, response) {
        var temp = $("input[name^='addSchool']").attr("name");
        var tempteacherID = temp.split(":");
        var teacherID;
        teacherID = tempteacherID[1]
        $.ajax({
            url: "JSONschools.asp",
            dataType: "json",
            data: { term: request.term, teacherID: teacherID },
            success: function (data) {
                response(data);
            }
        });
    },
    focus: CheckSuggestion,
    select: CheckSuggestion,
    change: CheckSuggestion,
    minLength: 2
});
于 2010-07-19T15:51:51.977 に答える
1

別の関数を作成し、次のことを参照します。

function SelectFocusChange(event, ui) { 
        //if the value of the textbox does not match a suggestion, clear its value 
        if (!ui.item) { 
            $(this).val(''); 
            $(this).parent("li").next().html("Please select only from the list shown").effect("pulsate", { times: 3 }, "slow"); 
        } 
        else { 
            $(this).next().val(ui.item.id); 
        } 
}



$("input[name^='addSchool']").autocomplete({ 
    source: function (request, response) { 
        var temp = $("input[name^='addSchool']").attr("name"); 
        var tempteacherID = temp.split(":"); 
        var teacherID; 
        teacherID = tempteacherID[1] 
        $.ajax({ 
            url: "JSONschools.asp", 
            dataType: "json", 
            data: { term: request.term, teacherID: teacherID }, 
            success: function (data) { 
                response(data); 
            } 
        }); 
    }, 
    focus: SelectFocusChange, 
    select: SelectFocusChange, 
    change: SelectFocusChange, 
    minLength: 2 
}); 
于 2010-07-19T15:52:48.340 に答える
1

関数を名前付き関数にして、以下で参照してください。

 function valueChanged(event, ui){
            //if the value of the textbox does not match a suggestion, clear its value
            if (!ui.item) {
                $(this).val('');
                $(this).parent("li").next().html("Please select only from the list shown").effect("pulsate", { times: 3 }, "slow");
            }
            else {
                $(this).next().val(ui.item.id);
            }
        }


$("input[name^='addSchool']").autocomplete({
    source: function (request, response) {
        var temp = $("input[name^='addSchool']").attr("name");
        var tempteacherID = temp.split(":");
        var teacherID;
        teacherID = tempteacherID[1]
        $.ajax({
            url: "JSONschools.asp",
            dataType: "json",
            data: { term: request.term, teacherID: teacherID },
            success: function (data) {
                response(data);
            }
        });
    },
    focus: valueChanged,
    select: valueChanged,
    change: valueChanged,
    minLength: 2
});
于 2010-07-19T15:54:35.107 に答える