jQuery get the value from checked radio input which wasn't checked before

时间:2017-04-10 01:50:23

标签: javascript jquery

I got the following HTML:

<input type="radio" name="eingangstuer_pfostenbefestigung" value="Zum Einbetonieren" />
<input type="radio" name="eingangstuer_pfostenbefestigung" value="Zum Einduebeln" />

Ater I check one of them and try to get the value with:

$("input[name=eingangstuer_pfostenbefestigung]:checked").val();

I get NaN, it only works when one of the the values is checked BEFORE. Are there any solutions for the js bug nr. 666?

When I simplify it with a much shorter but basically same way of reading it as the original code it works fine:

<input type="radio" name="eingangstuer_pfostenbefestigung" value="Zum Einbetonieren" />
<input type="radio" name="eingangstuer_pfostenbefestigung" value="Zum Einduebeln" />
<div style="" onclick="bla();">bla</div>

<script language="javascript" type="text/javascript">

function bla(){
    bla = $("input[name=eingangstuer_pfostenbefestigung]:checked").val();
    alert(bla);
}
</script>

Damn I don't know what to do now :/ I also get other values from other radio inputs this way in the org code but they are prechecked so it works fine, only when I have to check them manually before clicking bla it's not working

enter image description here

anzahl = $("input[name=eingangstuer_anzahl]").val();
muster = $("input[name=eingangstuer_muster]:checked").val();
breite = $("input[name=eingangstuer_breite]").val();
gesamtlaengeelement = breite * anzahl / 100;
hoehe = $("input[name=eingangstuer_hoehe]").val() - $("input[name=eingangstuer_bodenfreiheit]").val();
form = $("input[name=eingangstuer_form]:checked").val();
form = form.replace('eingangstuer_form_konvex', 'Konvex').replace('eingangstuer_form_normal', 'Normal').replace('eingangstuer_form_konkav', 'Konkav');
farbe = $("input[name=eingangstuer_farbe]:checked").val();
oeffnung = $("input[name=eingangstuer_oeffnung]:checked").val();
quadratmeter = breite * hoehe;


    $.extend(true, artikel,{
        [currentid]:{"anzahl":anzahl,
        "art":"Eingangstür",
        "betriebsart": "manuel",
        "muster": muster,
        "gesamtlaengeelement": gesamtlaengeelement,
        "hoehe": hoehe,
        "form": form,
        "farbe": farbe,
        "oeffnung": oeffnung}
    });



if(form == 'Normal')
{
    if(muster == 'L3 Modern')
    {
        multiplikator = 0.35;
    }
}

if(form == 'Konkav')
{
    if(muster == 'L3 Modern')
    {
        multiplikator = 0.4;
    }
}

if(form == 'Konvex')
{
    if(muster == 'L3 Modern')
    {
        multiplikator = 0.4;
    }
}

endpreis = quadratmeter * multiplikator * anzahl;
endpreis = endpreis / 10;



if(endpreis < 350)
{
    endpreis = 350;
}


if($('input[name="eingangstuer_e_oeffner"]:checked').length > 0)
{
    $.extend(true, artikel,{
        [currentid]:{"zubehoer": "E-&Ouml;ffner"}
    });

    oeffnerpreis = 65 * anzahl;
    endpreis = endpreis + oeffnerpreis;
}





pfostenanzahl = $("input[name=eingangstuer_pfosten_anzahl]").val();
if(pfostenanzahl > 0)
{
    $('#info_pfosten_anzahl').text(pfostenanzahl);
    $('#info_pfosten_art').html('T&uuml;rpfosten');
    $('#info_pfosten_farbe').text(farbe);


    pfostenmasse = $('#eingangstuer_pfosten_masse').find(":selected").val();

    pfostenlaenge = $("input[name=eingangstuer_pfosten_laenge]").val();

    $.extend(true, artikel,{
        [currentid]:{"pfostenmasse": pfostenmasse + 'x' + pfostenlaenge}
    });

    $('#info_pfosten_masse').text(pfostenmasse + 'x' + pfostenlaenge);


    befestigung = $("input[name=eingangstuer_pfostenbefestigung]:checked").val();

So here is the whole code of the function before the needed value. As mentioned: why is it working fine when I precheck the damn eingangstuer_pfostenbefestigung?

1 个答案:

答案 0 :(得分:0)

我找到了该死的答案。 这个功能造成了麻烦,浪费了5个小时的时间:

$("input").change(function(e) {
        var $this = $(this);
        var val = parseInt($this.val());
        var max = parseInt($this.attr("max"));
        var min = parseInt($this.attr("min"));

        var voreinstellung = parseInt($this.data("voreinstellung"));

        if(val >= min && val <= max)
        {

        }
        else
        {
              if (val > max)
              {
                e.preventDefault();
                $this.val(max);
              }
              else if (val < min)
              {
                e.preventDefault();
                $this.val(min);
              }
        }

        if(val > 0)
        {

        }
        else
        {
            e.preventDefault();
            $this.val(voreinstellung);
        }
});

所以我把它变成了$('input[type=number]') f ... javascript