简体   繁体   English

单选按钮选择会动态更改其他单选按钮的值

[英]Radio button selection dynamically change other radio button value

So I've tried looking, and I haven't found anything so hopefully this isn't a repeat question. 因此,我尝试查找,但未发现任何东西,因此希望这不是重复的问题。 I have several sets of radio buttons, and I need to have the values associated with the latter radio buttons change dynamically based on the users selection within the first set of radio buttons. 我有几组单选按钮,并且我需要让与后者的单选按钮相关联的值根据第一组单选按钮中的用户选择动态更改。

<input type="radio" name="length" id="6feet" value=" " >  6'0" 
<input type="radio" name="length" id="6.5feet" value=" " > 6'6"
<input type="radio" name="length" id="7feet" value=" " > 7'0"


<input type="radio" name="weight" ID="weight3" value="5" />  3 
<input type="radio" name="weight" ID="weight4" value="10" /> 4
<input type="radio" name="weight" ID="weight5" value="15" /> 5

<input type="radio" name="pieces" ID="PieceA" value="10">     2
<input type="radio" name="pieces" ID="PieceB" value="20">     3

So what I'm trying to figure out is if there is a way to use onClick or something similar to set it so that when the user selects one of the three "length" radio buttons, they will each assign different values to both the weight and pieces radio buttons as well. 所以我想弄清楚的是,是否有一种方法可以使用onClick或类似的方法进行设置,以便当用户选择三个“长度”单选按钮之一时,它们将分别为两个权重分配不同的值以及单选按钮。 Sorry if the question is unclear at all. 很抱歉,这个问题还不清楚。

A pure javascript solution would look similar to this: 一个纯JavaScript解决方案看起来类似于:

document.getElementById("6feet").onclick = (function() {
    document.getElementById("weight3").click();     
});

You attach the onclick event to an element (in this case the element with id of 6feet) and once that is clicked it calls the defined function. 您将onclick事件附加到一个元素(在本例中是ID为6feet的元素),一旦单击,它将调用已定义的函数。

Read more about .click() 进一步了解.click()

EXAMPLE

There are also simpler solutions using jQuery, but I wasn't sure if you were able to incorporate it into your code. 还有一些使用jQuery的更简单的解决方案,但是我不确定是否能够将其合并到代码中。

I didnt understand totally your question.. is this what you need? 我完全不明白您的问题。这是您需要的吗?

$(document).ready(function(){
    $('input[name=length]').click(function () {
        $('input[name=weight]').val(newValue);
        $('input[name=pieces]').val(newValue);        
    });
});

你需要像这样并重新分配一个真正的“价值”为单选按钮?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM