LoginSignup
1
1

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-05-11

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

どこにでもあるとおもいますが自分がわりと多く使うのでメモ.
選択肢によって送信フォームのボタンの状態を変えたい時に使えます.
特定の入力欄を入力していない場合にはボタンを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
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1