Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@Mabuchin

ラジオボタン選択肢で送信ボタンのアクティブ/非アクティブ状態を変更

ラジオボタンからアクティブにするボタンを変更する

どこにでもあるとおもいますが自分がわりと多く使うのでメモ.
選択肢によって送信フォームのボタンの状態を変えたい時に使えます.
特定の入力欄を入力していない場合にはボタンをdisabledにする等に応用が可能です.

注意:一部BootStrapを使用した表記になっています.

Javascript部分

<script type="text/javascript">
function changeDisabled() {
    if ( document.send_form["inlineRadioOptions"][1].checked ) {
        document . send_form["ng"] . disabled = false;
        document . send_form["ok"] . disabled = true;
    } else {
        document . send_form["ok"] . disabled = false;
        document . send_form["ng"] . disabled = true;
    }
}
window.onload = changeDisabled;
</script>

HTMLは下記のように記述します

    <form action="sendmai/result/" method="post" name="send_form" role=form>
        <div class='form-group'>
            <div class="has-success">
                <label class="radio-inline">
                    <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1" checked onClick="changeDisabled()"> YES!
                </label>
            </div class="has-success">
            <div class="has-error">
                <label class="radio-inline">
                    <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2" onClick="changeDisabled()"> NO!
                </label>
            </div class="has-error">
            <button type="submit" class="btn pull-right btn-success btn-lg" name="ok">Send</button>
            <button type="submit" class="btn pull-right btn-danger btn-lg" name="ng">Cancel</button>
        </div>
    </form>
1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What is going on with this article?