Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

どこにでもあるとおもいますが自分がわりと多く使うのでメモ.
選択肢によって送信フォームのボタンの状態を変えたい時に使えます.
特定の入力欄を入力していない場合にはボタンを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>
Why do not you register as a user and use Qiita more conveniently?
  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
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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