开发者

How do I combine multiple events in a jQuery UI autocomplete?

开发者 https://www.devze.com 2023-01-08 03:13 出处:网络
In the code below, I have the same function on three different events (focus, select, change). This seems redundant but I can\'t figure out how to combine the three into one. Thanks in advance for any

In the code below, I have the same function on three different events (focus, select, change). This seems redundant but I can't figure out how to combine the three into one. Thanks in advance for any ideas!

$("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,开发者_C百科 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
});


You can declare it once as named function, like this:

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);
  }
}

Then reference that function instead of an anonymous one, like this:

$("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
});


Create a separate function and refernece that:

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 
}); 


Make the function a named function and then refer to it below:

 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
});
0

精彩评论

暂无评论...
验证码 换一张
取 消