0

I need a simple code to disable the button to send the form to the following conditions:

If the selected <input type="radio"> with ID "#radio_one" and then check only one <input type="text"> with ID "#text_one".

OR

If the selected <input type="radio"> with ID "# radio_two" and then check two <input type="text"> with ID "#text_one" and also with ID "#text_two".

And, if provided with one or two <input type="text"> will be empty, add for a button <input type="send"> attribute disabled="disabled" if, however, will once again fill this attribute is removed from the button.

Sorry for my English, I used Google Translator: (

I dont have another code, I hope you understand me, so I created some simple jQuery code which can then possibly edit.

Thanks

Edit: This does not work:

    $("#send_button").attr("disabled", "disabled");   

    if ($('#absence_one').checked && $('#cal_from').val().length > 0) {
        $("#send_button").removeAttr("disabled");
    }

    if ($('#absence_more').checked && $('#cal_to').val().length > 0) {
        $("#send_button").removeAttr("disabled");
    }

2 Answers 2

1

Check out my example here jsfiddle.

I put a click listener on the radio to make it check to either disable or undisable the button

I put a keyup listener on the text input to make it check to either disable or undisable the button

EDIT

I made a slight change to the example here's the new one jsfiddle.

Also be sure you are double checking a submit on the server side. If someone disables javascript the button will be available for clicking.

$("#send_button").attr("disabled", "disabled");   

$("input[type='radio']").change(function()
{
    checkButton();
});

$("input[type='text']").keyup(function()
{
    checkButton();
});

function checkButton()
{
    if (($('#absence_one').is(":checked") && $('#cal_from').val().length > 0) || ($('#absence_more').is(":checked") && $('#cal_to').val().length > 0)) {
        $("#send_button").removeAttr("disabled");
    }
    else
    {
        $("#send_button").attr("disabled", "disabled");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand, you want to disable the submit button if either pair of radio button/textbox are unfulfilled. If so:

$("input[type=submit]").attr("disabled", "disabled");   

if ($('#radio_one').checked && $('#text_one').val().length > 0) {
    $("input[type=submit]").removeAttr("disabled");
}

if ($('#radio_two').checked && $('#text_two').val().length > 0) {
    $("input[type=submit]").removeAttr("disabled");
}

2 Comments

Yes you understand it well, but the code does not work. IDs are correct, but the button is still disabled after entering text. $("#send_button").attr("disabled", "disabled"); if ($('#absence_one').checked && $('#cal_from').val().length > 0) { $("#send_button").removeAttr("disabled"); } if ($('#absence_more').checked && $('#cal_to').val().length > 0) { $("#send_button").removeAttr("disabled"); }
try using .attr("disabled", "true") instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.