简体   繁体   English

如何使两个具有相同名称的单选按钮块在HTML中分开?

[英]How to make two blocks of radio buttons with the same name to be separated blocks in HTML?

I need to use two blocks of radio buttons for my project in which I'm storing the values in the Database and I'm showing one radio buttons block or the other depending on another result in the HTML form. 我需要在项目中使用两个单选按钮块,在其中将值存储在数据库中,并根据HTML表单中的另一个结果显示一个单选按钮块或另一个单选按钮块。

The problem is if I use the same name for all the options they are all part of the same group, which I don't want. 问题是,如果我对所有选项使用相同的名称,那么它们都属于同一组,而这是我所不希望的。

I just want to give it a try and ask if there's a way to group it in different blocks with the same name, is this even possible? 我只想尝试一下,然后问是否有办法将其分组为相同名称的不同块,这甚至可能吗?

This is my sample code in JSfiddle 这是我在JSfiddle中的示例代码

And mi code: 和mi代码:

<fieldset data-role="controlgroup" data-type="horizontal" >
    <legend>Negotiated:</legend>
    <input type="radio" name="radio-view" id="radio-view-a" value="0" checked="checked" />
    <label for="radio-view-a">No</label>
    <input type="radio" name="radio-view" id="radio-view-b" value="1" />
    <label for="radio-view-b">Yes</label>
</fieldset>


<fieldset data-role="controlgroup" data-type="horizontal" >
    <legend>Negotiated:</legend>
    <input type="radio" name="radio-view" id="radio-view-a1" value="0" />
    <label for="radio-view-a1">No</label>
    <input type="radio" name="radio-view" id="radio-view-b1" value="1" checked="checked" />
    <label for="radio-view-b1">Yes</label>
</fieldset>

Your problem can easily be solved, if you give them same classes. 如果给他们相同的课程,则可以轻松解决您的问题。 Change your code to this : 将代码更改为此:

<fieldset data-role="controlgroup" data-type="horizontal" >
<legend>Negotiated:</legend>
<input type="radio" name="radio-view1" class="radio-view" id="radio-view-a" value="0" checked="checked" />
<label for="radio-view-a">No</label>
<input type="radio" name="radio-view1" class="radio-view" id="radio-view-b" value="1" />
<label for="radio-view-b">Yes</label>
</fieldset>


<fieldset data-role="controlgroup" data-type="horizontal" >
<legend>Negotiated:</legend>
<input type="radio" name="radio-view2" class="radio-view" id="radio-view-a1" value="0" />
<label for="radio-view-a1">No</label>
<input type="radio" name = "radio-view2" class="radio-view" id="radio-view-b1" value="1" checked="checked" />
<label for="radio-view-b1">Yes</label>
</fieldset>

JSFiddle JSFiddle

Or if for some reason you don't want to use class then you can use data-customclass attribute, your code will look like this : 或者如果由于某种原因您不想使用class那么可以使用data-customclass属性,您的代码将如下所示:

<fieldset data-role="controlgroup" data-type="horizontal" >
<legend>Negotiated:</legend>
<input type="radio" name="radio-view1" data-customclass="radio-view" id="radio-view-a" value="0" checked="checked" />
<label for="radio-view-a">No</label>
<input type="radio" name="radio-view1" data-customclass="radio-view" id="radio-view-b" value="1" />
<label for="radio-view-b">Yes</label>
</fieldset>


<fieldset data-role="controlgroup" data-type="horizontal" >
<legend>Negotiated:</legend>
<input type="radio" name="radio-view2" data-customclass="radio-view" id="radio-view-a1" value="0" />
<label for="radio-view-a1">No</label>
<input type="radio" name="radio-view2" data-customclass="radio-view" id="radio-view-b1" value="1" checked="checked" />
<label for="radio-view-b1">Yes</label>
</fieldset>

JSFiddle JSFiddle

In the second approach, you can use any attribute prefixed with data- . 在第二种方法中,可以使用任何以data-为前缀的属性。 For example, you can also use data-myclass . 例如,您也可以使用data-myclass

But don't include uppercase letters,they are not allowed. 但不要包含大写字母,这是不允许的。 If you are using any framework such as jQuery Mobile then don't use its reserved data- attributes such as data-role . 如果您使用的是诸如jQuery Mobile类的任何框架,请不要使用其保留的data-role data-属性,例如data-role Prefixing your attribute with 'my' is a good practice, for example, data-myclass . 用'my'前缀属性是一种很好的做法,例如data-myclass

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

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