LoginSignup
0
1

More than 3 years have passed since last update.

disabled属性のフィールドのデータ送信方法

Posted at

超絶初級レベルなんでしょうが、躓いたので備忘録としてdisabled属性のフィールドのデータを送信する方法をメモメモ、、、、。

やりたかったこと&躓いたこと

複数のラジオボタンのうち、1つのラジオボタンだけ権限付けをしておいて、権限がない人は、そのラジオボタンを選択できない。
ラジオボタン以外にもテキストエリアがあって、そのテキストエリアはだれでも編集できる。
権限がある当該のラジオボタンにif文でdisabled属性を付けておいた。
しかし、初めから権限付きラジオボタンが選択されていて、権限がない人がテキストエリアを編集しようとすると、ラジオボタンの情報が送信されないので、ぬるぽ、、、。

disabled

formのフィールドにdisabled属性を付けると、無効状態になって入力できない&データの送信もできない様になる。

jsで送信直前にdisabled属性を解除

送信ボタン押下でdisabledを解除する。但しこれは、押下後画面遷移するまで時間がかかる場合、disabledだった項目が選択(入力)できるような状態になってしまうので、これじゃダメな場合もある。(解除の方法をネットで調べると.disabled=falseとか出てくるが、私はそれではできなかった。)

<input type="radio"   name="radio" id="radio" value="送信内容" disabled="disabled" /> 
<input type="submit" value="送信" onclick="javascript:unDisabled();" />
<script type="text/javascript">
            function unDisabled()
            {
                $('.radio').removeAttr("disabled");

            }
        </script>

送信直前にdisabledの値をhiddenにコピー

ただし、今回のように権限付けしてあってdisabledが動的に決まるものだと、同じ内容のデータを2度送信してしまう可能性がある。これの回避は結構めんどくさそう。(いい方法があるのか分らんが、、、)

<script type="text/javascript">
            function text_copy()
            {
                document.formMain.text2.value = document.formMain.text1.value ;
                return true;
            }
        </script>
<input type="hidden"   name="text2" id="hidden_text" value="" />
            <input type="radio"   name="radio" id="radio" value="送信内容" disabled="disabled" />
            <input type="submit" value="送信" onclick="text_copy();" />

readonlyにしてcssで選択不可にする

こういう方法もあるらしい。

参考

0
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
0
1