DiniZx DiniZx - 1 year ago 72
Javascript Question

How to avoid radio button deselected automatically.

There are two radio button. when select any radio button its automatically deselected.

Following are the snippet which is not working properly.



$('.radio_pmt').click(function(){
alert('click');
var payment_mode =$(this).attr('ownid');

var type = typeof payment_mode;
if(payment_mode == 'undefined' | payment_mode == null){
alert("Please select payment method");
}
else
{
if(payment_mode == "paypal")
{
$('input:radio[name="payment_mode"][ownid="paypal"]').attr('checked', true);
$('input:radio[name="payment_mode"][ownid="paypal"]').val('paypal');
$("#paypal_form").submit();
}
else
{
$('input:radio[name="payment_mode"][ownid="braintree"]').val('braintree');
$('input:radio[name="payment_mode"][ownid="braintree"]').attr('checked');
$("#braintree_details").slideDown("slow");

}
}
return false;
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" id="frm_choose_plan" name="frm_choose_plan">
<label>Choose Payment method:</label>
<input type="radio" class="radio_pmt" ownid="paypal" name="payment_mode" value="Paypal">Paypal
<input type="radio" class="radio_pmt" ownid="braintree" name="payment_mode" value="braintree" id="credit_cardpmt">Credit card
</form>




Answer Source

Remove the return false; line and your code will work. It is deselecting because you are preventing the default action of radio button

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download