“その他”を追加するにはどうすればよいですかHTMLフォームの一連のラジオボタンへのテキスト入力

StackOverflow https://stackoverflow.com/questions/1228504

質問

書き込み「その他」を使用してフォームを作成しようとしています最後のラジオボタンとしてオプション。次のバリエーションを試してみましたが成功しませんでした:

<label><input type="radio" name="candidate" value="option1">First Option</label>
<label><input type="radio" name="candidate" value="option2">Second Option</label>
<label><input type="radio" name="candidate" value="other">OTHER</label><input type="text" name="other" value="Write In" />

javascriptを使用せずに送信時にラジオボタンに値を渡すテキスト入力を持つラジオボタンを使用できますか?

役に立ちましたか?

解決

「その他」が選択されている場合、テキストフィールドの値のみを送信する必要があります。それをラジオにフィードしてサーバーに渡すことは意味がありません。 「その他」ラジオの値に空の文字列を指定できます。例:

<label><input type="radio" name="candidate" value="">OTHER</label>

また、&quot; candidate&quot;に値が存在しない場合はサーバー上で「その他」に注目してください。

他のヒント

いいえ-これはHTML仕様ではサポートされていません。 フォームを送信する前にjavascriptを使用してそれを行うか、ラジオボタンが「その他」に設定されているかどうかを確認することをお勧めします。そして、フォームが送信された後にテキストボックスの値を読み取ります。

「その他」の戻り値を変更することで、本当に混乱したくない理由の1つは、ラジオボタンは、「option1」と入力するとどうなりますか。テキストボックスに入力しますか?

HTML仕様がこれをサポートしているとは思わない、いや。処理コードにラジオボタンを見てもらい、値が「その他」であることを確認し、テキストフィールドの入力から値を取得する必要があります(「候補」の代わりにフォーム送信の「その他」変数'var)。

いいえ。これには、JavaScriptを必要とするDOMの動的更新が必要です。

これは私が見ることができる最も簡単な方法です。間違っている場合は修正してください!

<form name="fruitForm" method="post" action="other-post.php">
<label><input type="radio" name="fruit" value="apple" onClick="regularFruit()">apple</input></label>
<label><input type="radio" name="fruit" value="orange" onClick="regularFruit()">orange</input></label>
<label><input type="radio" name="fruit" value="lemon" onClick="regularFruit()">lemon</input></label>
<label onClick="otherFruit()">
    <input type="radio" name="fruit" id="other_fruit" value="other" >or other fruit:</input>
    <input type ="text" name="other" id="other_text"/></label>
    <input type="submit" value="Submit">
</form>

<script>
function otherFruit(){
a=document.getElementById('other_fruit');
a.checked=true;
}
function regularFruit(){
a=document.getElementById('other_text');
a.value="";
}
</script>

ラベルにラジオとテキストフィールドが組み込まれている場合、テキストフィールドは「その他」をチェックします。ラジオ、および「その他」の値を送信します。フィールドに入力されるものとして。次に、ラジオボタンをもう一度チェックすると、テキストフィールドがクリアされます

これが私がしたことです(スニペットを実行して、それが何をするかを確認します)。うまく行けば、その仕組みを理解できます。

フォームを処理するとき、ラジオボタンがチェックされる 、またはテキストボックスに値がある という事実によって値をチェックできます。

$(document).ready(
  function() {
    $('input[name=amount]').on('click', function() {
      var customText = $('input[name=custom-amount]');
      customText.val('');
      customText.parent().removeClass("selected");
    });

    $('input[name=custom-amount]').on('click', function() {
      $('input[name=amount]').attr('checked', false);
      $(this).parent().addClass("selected");
    });
  });
.donate {
  font-family: sans-serif;
}
.donate .payee {
  color: #4B8EBC;
  font-weight: bold;
}
.donate .custom-amount {
  width: 150px;
  height: 40px;
  background-color: #7DB6DA;
  color: #325F7F;
  font-weight: bold;
  font-size: 1rem;
  padding: 2px;
  padding-left: 6px;
}
.donate .custom-amount input {
  font-weight: lighter;
  font-size: 0.8rem;
  background-color: white;
  width: 116px;
  height: 24px;
  margin-top: 4px;
  margin-left: 6px;
}
.donate .radio-control {
  position: relative;
  display: inline-block;
  font-size: 1rem;
  width: 50px;
  height: 40px;
  cursor: pointer;
  z-index: 12;
}
.donate .radio-control input[type="radio"] {
  position: absolute;
  z-index: -1;
  opacity: 0;
}
.donate .radio-control__indicator {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  text-align: center;
  line-height: 40px;
}
.donate .radio-control__indicator, .donate .custom-amount {
  background-color: #7DB6DA;
  color: #325F7F;
}
.donate .radio-control:hover input ~ .radio-control__indicator,
.donate .radio-control input:focus ~ .radio-control__indicator {
  opacity: 0.9;
}
.donate .radio-control input:checked ~ .radio-control__indicator,
.donate .custom-amount.selected {
  background: #4B8EBC;
  color: white;
}
.donate .radio-control:hover input:not([disabled]):checked ~ .radio-control__indicator,
.donate .radio-control input:checked:focus ~ .radio-control__indicator {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="donate">
  <div class="row amount">
    <div class="control-group">
      <label class="radio-control">
        <input checked="checked" name="amount" type="radio" value="$5" />
        <div class="radio-control__indicator">
          $5
        </div>
      </label>
      <label class="radio-control">
        <input name="amount" type="radio" value="$10" />
        <div class="radio-control__indicator">
          $10
        </div>
      </label>
      <label class="radio-control">
        <input name="amount" type="radio" value="$20" />
        <div class="radio-control__indicator">
          $20
        </div>
      </label>
      <label class="radio-control">
        <input name="amount" type="radio" value="$50" />
        <div class="radio-control__indicator">
          $50
        </div>
      </label>
      <div class="custom-amount">
        $
        <input class="custom-amount" name="custom-amount" placeholder="Custom amount" size="40" />
      </div>
    </div>
  </div>
</form>

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top