我如何在HTML表单中的“其他”文本输入添加到组单选按钮?
-
22-07-2019 - |
题
我试图创建一个写在“其他”选项,最后一个单选按钮的形式。我试过没有成功以下的变体:
<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的文本输入?
解决方案
如果“其他”被选中,你想在文本字段只提交值时,它没有任何意义饲料是进入广播和传递到服务器。你可以给“其他”无线电空字符串,e.g的值:
<label><input type="radio" name="candidate" value="">OTHER</label>
和在服务器上,如果没有值存在于“候选”看到“其他”。
其他提示
没有 - 这不是由HTML规范的支持。 你要么需要通过JavaScript做它之前提交的形式,或者更好的是,检查单选按钮被设置为“其他”,并在此之后提交表单读取文本框的值。
您不想真的改变“其他”单选按钮的返回值勾搭一个原因是什么,如果我在你的文本框中输入“选项1”?
我不相信HTML规范支持这一点,没有。你只是必须有你的处理代码看看单选按钮,将看到该值是“其他”,并去获得从文本字段输入(值在表单提交,而不是“候选人“其他”变种'VAR)。
没有。这就要求动态更新到DOM,这需要的JavaScript。
这是我看到的最简单的方法,请纠正我,如果我错了!
<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>
不隶属于 StackOverflow