开发者

Deleting an option in a <select> list

i have following function:

function delete_auswahl()    
{

    var anzahl =document.getElementById ("warenkorbfeld").length ;

    for (var i =0; i<=anzahl; i++)
    {
        if (document.getElementById ("warenkorbfeld").options[i].selecte开发者_Go百科d==true)
        {



            if (document.getElementById ("warenkorbfeld").options[i].id == "Margherita" )
                gesamtbetrag = gesamtbetrag - 4;

            if (document.getElementById ("warenkorbfeld").options[i].id=="Salami"  )
                gesamtbetrag = gesamtbetrag - 4.50;

            if (document.getElementById ("warenkorbfeld").options[i].id=="Hawaii"  )
                gesamtbetrag = gesamtbetrag - 5.50;


            document.getElementById ("warenkorbfeld").options[i]=null;
            i--;// auf der gleichen stelle bleiben, da dass nächste feld nachrückt

        }
    }

    document.getElementById('gesamtbetrag').innerHTML=gesamtbetrag ;

}

before i added values with

function hinzu (pizza)
{
    NeuerEintrag = new Option(pizza, pizza, false, false);
    document.getElementById("warenkorbfeld").options[document.getElementById("warenkorbfeld").length] = NeuerEintrag ;

    if (pizza=="Margherita")
    {
        gesamtbetrag = gesamtbetrag + 4;
    }

    if (pizza=="Salami")
    {
        gesamtbetrag = gesamtbetrag + 4.50;
    }

    if (pizza=="Hawaii")
    {
        gesamtbetrag = gesamtbetrag + 5.50;
    }

    document.getElementById('gesamtbetrag').innerHTML=gesamtbetrag ;

}

now, in the delete function doesn't substract the price. despite this, all works.

what's wrong with this term?

if (document.getElementById ("warenkorbfeld").options[i].id == "Margherita" )
                gesamtbetrag = gesamtbetrag - 4;

thanks in advance


Where does the gesamtbetrag variable come from? You're subtracting from it but it looks like it doesn't get defined until afterward. I think you may need to add

var gesamtbetrag = document.getElementById('gesamtbetrag').innerHTML

To the top of your function.

Also, consider the following optimisations for your code:

  • you don't need the for loop to iterate over the options (for (var i =0; i<=anzahl; i++), you can use the selectedIndex property of the select element instead.
  • Use select.remove() instead of select.options[i] = null.
  • Make use the subtraction assignment operator -= to subtract a number for the variable.
  • Reduce getElementById lookups by storing the element in a variable instead.
  • You could compare the selectedIndex to a known index (e.g. sel.selectedIndex == 0) instead of comparing selectedOption.id == "Some string". I haven't done this below because I don't know your indexes
function delete_auswahl()    
{
    var gesamtbetrag = document.getElementById('gesamtbetrag').innerHTML;
    var selEl = document.getElementById("warenkorbfeld");
    var opt   = selEl.options[selEl.selectedIndex];

    if (opt.id == "Margherita")
        gesamtbetrag -= 4;
    else if (opt.id=="Salami")
        gesamtbetrag -= 4.50;
    else if (opt.id=="Hawaii")
        gesamtbetrag -= 5.50;

    selEl.remove(selEl.selectedIndex);
}


i've got the problem, it was

if (document.getElementById ("warenkorbfeld").options[i].value=="Salami" )

options.value

not

options.id

now it works :-)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜