如何使用javascript删除最后添加的<input />

时间:2014-07-30 19:11:47

标签: javascript html5

我正在尝试创建一个表单,用户可以根据需要添加和删除文本输入。我已经制作了一些代码来添加文本输入,但我无法弄清楚如何删除新添加的文本输入。

我的JavaScript如下:

function addInput() {
    var antal = document.getElementById('antalfelter').value;
    var person = document.getElementById('persondiv');
    antal++;

    person.innerHTML = person.innerHTML
                       + '<input type="text" id="person' + antal + '" name="person' + antal + '"'
                       + 'style="width: 200px; height: 25px; margin-left: 15px;" value="' + antal
                       + '"><br>';                    

    document.getElementById("antalfelter").value = antal;

我的HTML:

<form method="post" action="opretreferat.php" id="tilstede">
    <input type="hidden" id="antalfelter" name="antalfelter" value="1">
    <div id="persondiv">
        <input type="text" id="person" name="person1" value="1"><br>
    </div>
    <input type="button" id="add" name="add"
           value="Tilføj felt" onclick="addInput()"><br>
    <input type="button" id="remove" name="remove"
           value="Fjern felt" onclick="removeInput()"><br><br>
    <input type="submit" id="next" name="next"
           value="Næste">
</form>

我想删除最后一个文本输入,除非它是唯一剩下的文本输入。

任何人都可以帮助我吗?

(对不起我的丹麦语标记。我希望你能看透它。)


修改

我已将JavaScript改为此以摆脱换行符:

function addInput() {
    var antal = document.getElementById('antalfelter').value;
    var person = document.getElementById('persondiv');
    antal++;

    person.innerHTML = person.innerHTML
                       + '<div id="persondiv' + antal + '">'
                       + '<input type="text" id="person' + antal + '" name="person' + antal + '"'
                       + 'style="width: 200px; height: 25px; margin-left: 15px;" value="' + antal
                       + '"></div>';                    

    document.getElementById("antalfelter").value = antal;
}

4 个答案:

答案 0 :(得分:2)

你可以试试这个:

function removeInput() {
    var allInputs = document.getElementById('persondiv').querySelectorAll('input[type="text"]'),
        totalInputs = allInputs.length;
    if (totalInputs > 1) {
        allInputs[totalInputs - 1].parentNode.removeChild(allInputs[totalInputs - 1]);
    }
}

DEMO

答案 1 :(得分:1)

如果您动态构建DOM,而不是使用innerHTML,这将有所帮助。但是,仍然可以这样:

function removeLastInput()
{
    //  find all <input> tags:
    var inputs = document.getElementById('persondiv').getElementsByTagName("input")

    //  find the last one that's valid:
    var i = inputs.length
    do {
        i --
        var lastInput = inputs[i]
    } while(i > 0 && lastInput.type !== "text")

    //  if the correct one wasn't found, quit:
    if(i <= 0 || lastInput.type !== "text") return;

    //  remove it via the parentNode:
    lastInput.parentNode.removeChild(lastInput)
}

答案 2 :(得分:1)

这是我创建的JsFiddle,可能有助于您尝试做的事情。我还向前迈了一步,让你能够删除列表中的任何人输入字段。我试着记录下我能做的最好的事情来帮助你理解我在做什么并试图完成。希望这可以帮助。

以下是代码的一小部分:

function removeLastPerson(){
     var lastIndex = getNumberOfPeople() - 1;
     removePerson(lastIndex);
}

function getNumberOfPeople(){
    //this is counting all of the input fields inside of the person div.
    return $("#person input").size();
}

function removePerson(index){
    if ( getNumberOfPeople() === 1 ){
        alert("Cannot remove the last person input box");
        return false;
    }

    if (index < 0){
        alert("There are no people to remove.");
        return false;
    }

    //instead of using <br /> tags i used div tags since div tags do make a new line.  When you remove a person input the <br /> tags were left.
    $("#person_" + index + "_tag").remove(); 
    reIndex(); //reindex is needed when removing a specific person input field in a list of input fields.  See JsFiddle to see what is going on in the reIndex function
}

答案 3 :(得分:0)

我已经摆弄了一些代码并且我试图保持简单并且最后(在阅读Rahil Wazir回答他使用parentNode的地方之后)我想出了这个:

HTML:

<form method="post" action="opretreferat.php" id="tilstede">
    <input type="hidden" id="antalfelter" name="antalfelter" value="1">
    <div id="persondivcontainer">
        <div id="persondiv1">
            <input type="text" id="person1" name="person1"
                   style="width: 200px; height: 25px; margin-left: 15px;"
                   value="1">
        </div>
    </div>
    <input type="button" id="add" name="add"
           style="width: 200px; height: 25px; margin-left: 15px;"
           value="Tilføj felt" onclick="addInput()"><br>
    <input type="button" id="remove" name="remove"
           style="width: 200px; height: 25px; margin-left: 15px;"
           value="Fjern felt" onclick="removeInput()"><br><br>
    <input type="submit" id="next2" name="next2"
           style="width: 200px; height: 25px; margin-left: 15px;"
           value="Næste">
</form>

我的JavaScript:

function addInput() {
    var antal = document.getElementById('antalfelter').value;
    var person = document.getElementById('persondiv' + antal);

    if (antal < 6 ) {
        antal++;

        person.outerHTML = person.outerHTML
                           + '<div id="persondiv' + antal + '">'
                           + '<input type="text" id="person' + antal + '" name="person' + antal + '"'
                           + 'style="width: 200px; height: 25px; margin-left: 15px;" value="' + antal
                           + '"></div>';                    

        document.getElementById("antalfelter").value = antal;
    }
}

function removeInput() {
    var antal = document.getElementById('antalfelter').value;                    
    var person = document.getElementById('persondiv' + antal);

    if (antal > 1) {
        person.parentNode.removeChild(person);
        antal--;
    }

    document.getElementById("antalfelter").value = antal;
}

......它可以正常工作。

我为您的所有回复感到高兴,并且通过我的第一个JavaScript帮助我朝着正确的方向前进。

谢谢大家