回发后返回下一个tabindex

My user is presented with generic names: name1, name2, name3 and is asked to change each to more meaningful names. How can I make the ajax post return go to the next input/tabindex or at least not return to the name just changed? (Look for WHAT GOES HERE? comment below)

HTML:

    <div class="name"><input type="text" id="nameinput1" value="name1" onchange="changeName(0)"></div>
    <div class="name"><input type="text" id="nameinput2" value="name2" onchange="changeName(1)"></div>
    <div class="name"><input type="text" id="nameinput3" value="name3" onchange="changeName(2)"></div>

javascript:

    var jsons = '[{' ;
    var jsone = '"}]' ;
    function changeName(index){
    var json = jsons;
    var name = document.getElementById("main").getElementsByTagName("input")[index].value;
                json += '"index":"'+index+'",'
                json += '"name":"'+name.replace(/\s/g,"")+jsone
                loadDoc(json,"ID","url",returnChangeName)
            }       
    function returnChangeName(xttp){
    window.location = "#";    //  WHAT GOES HERE?
}            
    loadDoc(...){....}

纯JavaScript,请不要使用jquery。

评论
  • 冼华美
    冼华美 回复

    All you need to do is to pass the current index to the callback function returnChangeName and you can get the next input in the callback. I also recommend you using the JSON.stringify function for converting objects to string:

    function changeName(index) {
        var name = document.getElementById("main").getElementsByTagName("input")[index].value;
        var json = [{
            index: index,
            name: name.replace(/\s/g, "")
        }];
        loadDoc(JSON.stringify(json), "ID", "url", returnChangeName(index));
    }
    
    function returnChangeName(index) {
        return function returnChangeNameResponse(xttp) {
            var next = document.getElementById("main").getElementsByTagName("input")[index + 1];
            if (next) {
                // whatever you want to do with this
                // for example, focus the next one
                next.focus();
            }
        }
    }
    
    function loadDoc(json, id, url, callback) {
        // sth
        callback({});
    }