0

我目前正在修改一个开箱即用的 php/jQuery 自动建议脚本。我正在努力让脚本与多个文本输入字段一起工作。它最初是为与 ONE 字段一起工作的。

这是原始脚本和表单代码:

function suggest(inputString){
    if(inputString.length == 0) {
        $('#suggestions').fadeOut();
    } else {
    $('#city').addClass('load');
        $.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){
            if(data.length >0) {
                $('#suggestions').fadeIn();
                $('#suggestionsList').html(data);
                $('#city').removeClass('load');
            }
        });
    }
}

function fill(thisValue) {
    $('#city').val(thisValue);
    setTimeout("$('#suggestions').fadeOut();", 0);
}

我相信,修改脚本以使用多个输入字段的方法是获取焦点表单元素的 id,然后执行以下操作:

function getFocusID() {
    // get the focused form field id here
}

if(focusedID == "city") {
    //Script for form field with "city" as id

    function suggest(inputString){
        if(inputString.length == 0) {
            $('#suggestions').fadeOut();
        } else {
        $('#city').addClass('load');
            $.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){
                if(data.length >0) {
                    $('#suggestions').fadeIn();
                    $('#suggestionsList').html(data);
                    $('#city').removeClass('load');
                }
            });
        }
    }

    function fill(thisValue) {
        $('#city').val(thisValue);
        setTimeout("$('#suggestions').fadeOut();", 0);
    }
} // code for input id "city" END

// And now create if else statements for the other 4 input text fields, filling them with the code above.

我在这里想对了吗?或者有没有其他更有效的方法来做到这一点?如果这是正确的方法,我如何获得重点输入文本字段的输入 ID?

4

1 回答 1

1

那将是activeElement

function getFocusID() {
    return document.activeElement.id;
}
于 2013-04-02T16:22:14.570 回答