Radio Button from Ajax does not work

Asked

Viewed 40 times

0

Hello I have a radio button q comes from ajax for calculation of front not it not worked to take the . attr({disabled: true}) from a button.

If I put it right on the page it works, but when it loads from an ajax request. No.

Inputs

<label class="radio">
  <input type="radio" id="frete_radio" name="frete_radio" value="1"> 
  <span>a</span>
</label>
<label class="radio">
  <input type="radio" id="frete_radio" name="frete_radio" value="2"> 
  <span>b</span>
</label>

If I load input:radio from ajax it does not make the code below work.

But if I put it right on the page it works.

Knob

<button type="submit" id="btn-finalizar" disabled="disabled">

Code:

<script type="text/javascript">
    $(document).ready(function() {  
        $("input[type=radio]").bind("click", function(){        
            if($("input[type=radio]:checked").val() == "frete_radio") {
                $("#btn-finalizar").attr({disabled: true});
            } else {
                $("#btn-finalizar").attr({disabled: false});
            }
        }); 
    });
</script>

1 answer

1


If the elements are being inserted into the DOM after the click bind, you need to delegate the click so that jquery can identify the clicked element.

You can do this with the method .on() which is available on jQuery since its version 1.7

<script type="text/javascript">
    $(document).ready(function() {  
        $(document).on("click", "input[type=radio]", function(){        
            if($("input[type=radio]:checked").val() == "frete_radio") {
                $("#btn-finalizar").attr({disabled: true});
            } else {
                $("#btn-finalizar").attr({disabled: false});
            }
        }); 
    });
</script>

In versions prior to 1.7 you can get the same result with the method .delegate() which is available since version 1.4.2 and is deprecated since version 3.0

In versions prior to 1.4.2 the same result can be obtained with the method .live() which is available since version 1.3 and has been deprecated in version 1.7 and removed from core in version 1.9.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.