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 回答