链接单选按钮和文本输入
我正在尝试将无线电输入与指定的文本输入相关联。正如您在图片中看到的,有两种选择。我知道我可以使用 for=""
将标签关联到无线电输入,但是我如何才能将它关联到下面的文本输入,反之亦然,所以当我专注于文本输入时,是否聚焦正确的单选按钮?
注意:输入的金额将被插入到数据库中,因此这是此表单中最重要的部分。目前,如果我点击 $Off,我仍然可以在 %Off 中输入数字。我不希望这种情况发生,这样用户就不会感到困惑。
我的标记:
<div class="row-fluid control-group">
<div class="span7 pull-left">
<label class="radio" for="discount-dollars">
<input type="radio" name="discount" id="discount-dollars" value="dollars" checked="checked">
$ Off
</label>
<div class="input-append">
<input type="text" name="discount-dollars-amount" id="discount-dollars-amount" class="input-small dollars" placeholder="enter amount">
<span class="add-on">.00</span>
</div>
</div><!-- .span7 .pull-left -->
<div class="span5">
<label class="radio" for="discount-percent">
<input type="radio" name="discount" id="discount-percent" value="percent">
% Off
</label>
<input type="text" name="discount-percent-amount" id="discount-percent-amount" class="input-small percent" placeholder="enter amount" disabled="disabled">
</div>
</div><!-- .row-fluid .control-group -->
<script type="text/javascript">
$(function (){
$("form input[type=radio]").click(function (){
// get the value of this radio button ("dollars" or "percent")
var value = $(this).val();
// find all text fields...
$(this).closest("form").find("input[type=text]")
// ...and disable them...
.attr("disabled", "disabled")
// ...then find the text field whose class name matches
// the value of this radio button ("dollars" or "percent")...
.end().find("." + value)
// ...and enable that text field
.removeAttr("disabled")
.end();
});
});
</script>
I am trying to associate a radio input with a specified text input. As you can see in the picture, there are two options. I know I can use for=""
to associate the label to the radio input, but how can I also associate it to the text input underneath, and vise versa so when I focus on the text input, if focuses the correct radio button?
NOTE: The amount entered will be inserted into the database, so that's the most important part of this form. Currently if I click on $Off, I can still enter a number in %Off. I don't want this to happen, so the user does not get confused.
My markup:
<div class="row-fluid control-group">
<div class="span7 pull-left">
<label class="radio" for="discount-dollars">
<input type="radio" name="discount" id="discount-dollars" value="dollars" checked="checked">
$ Off
</label>
<div class="input-append">
<input type="text" name="discount-dollars-amount" id="discount-dollars-amount" class="input-small dollars" placeholder="enter amount">
<span class="add-on">.00</span>
</div>
</div><!-- .span7 .pull-left -->
<div class="span5">
<label class="radio" for="discount-percent">
<input type="radio" name="discount" id="discount-percent" value="percent">
% Off
</label>
<input type="text" name="discount-percent-amount" id="discount-percent-amount" class="input-small percent" placeholder="enter amount" disabled="disabled">
</div>
</div><!-- .row-fluid .control-group -->
<script type="text/javascript">
$(function (){
$("form input[type=radio]").click(function (){
// get the value of this radio button ("dollars" or "percent")
var value = $(this).val();
// find all text fields...
$(this).closest("form").find("input[type=text]")
// ...and disable them...
.attr("disabled", "disabled")
// ...then find the text field whose class name matches
// the value of this radio button ("dollars" or "percent")...
.end().find("." + value)
// ...and enable that text field
.removeAttr("disabled")
.end();
});
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能使用单个
元素来标记两个单独的输入。我建议将标签与单选按钮相关联,因为单选按钮是一个很小的点击目标,而标签会扩展该目标。
选择默认选择的无线电之一,可能是“$ Off”。默认情况下禁用其他文本字段:
然后使用 jQuery 执行类似以下操作:
基本上,这会侦听两个单选按钮上的
click
事件。当您单击一个单选按钮时,它会启用其关联的文本字段(即 CSS 类名称与单选按钮的值匹配的文本字段)并禁用其他文本字段。这样,除非选中其关联的单选按钮,否则您无法在任一文本字段中输入文本。You can't use a single
<label>
element to label two separate inputs. I would suggest associating the labels to the radio buttons, since the radio button is such a small click target and the label expands that target.Choose one of the radios to be selected by default, perhaps "$ Off". Disable the other text field by default:
Then use jQuery to do something like this:
Basically, this listens for
click
events on both radio buttons. When you click one radio, it enables its associated text field (i.e., the text field with a CSS class name matching the value of the radio button) and disables the other text field. That way, you can't enter text into either text field unless its associated radio button is checked.使用具有 2 种状态的图像单选按钮,选中和未选中,当您单击单选图像时,只需将焦点设置到文本框并交换到所选的单选图像,反之亦然。
use image radio button with 2 states, selected and not selected, when you click the radio image just set focus to the textbox and swap to your selected radio image, and vice versa.
您不能专注于两个元素,它将是输入文本字段或单选按钮。
但是,当您单击单选按钮以关注下面的字段时,或者当您在下面的字段中输入值以检查上面的单选按钮时,您应该使用 JavaScript 或 jQuery。
如果您想使用jquery,这可以帮助您: http://api.jquery.com/val/ 和 http://api.jquery.com/focus/
You cannot focus on two elements, it will be either the input text field or the radio button.
But you should use JavaScript or jQuery, when you click on the radio button to focus on the field below, or when you enter value in the field below to check the radio button above.
This can help you if you want to use jquery : http://api.jquery.com/val/ and http://api.jquery.com/focus/
这是一个非常粗略的草图,可以帮助您,但是您可以这样做(让您遵循一定的命名约定):
This is a VERY rough sketch to help you out, but you could do something like this (giving you follow a certain naming convention):
jQuery 就是你想要的。假设您的元素具有如下 ID:
...代码可能如下所示。这将负责禁用文本框并正确聚焦输入。
jQuery is what you want. Assuming your elements had IDs as follows:
...the code might look something like this. This will take care of disabling text boxes and focusing input properly.