单击标签上的单选按钮选择

发布于 2024-08-07 05:20:40 字数 1211 浏览 1 评论 0原文

当我单击单选按钮前面的两个标签中的任何一个时,如何使单选按钮被选中?

我有以下单选按钮代码:

<div class="label_main" style="width:350px">
    <div class="label_radio">
      <input type="radio" name="group1" id="group1" value="0" 
       <?php if($r0==0) { ?>
       checked="checked"
       <?php  } ?>
      />
    </div>
    <div class="label_top">
      <label for="group1">First Option </label>
    </div>
    <div class="label_desc">
      <label for="group1">This is first option </label>
    </div>
  </div>
      </div> 

  <div class="label_main" style="width:350px">
    <div class="label_radio">
      <input type="radio" name="group1" id="group1" value="1"
       <?php if($r0==1) { ?>
       checked="checked"
       <?php  } ?>
      />
    </div>
    <div class="label_top">
      <label for="group1">Second Option </label>
    </div>
    <div class="label_desc">
      <label for="group1">This is Second Option </label>
    </div>
  </div>          

如果我更改单选 ID,我遇到的唯一问题是它工作正常,但我需要相同的 id,因为我需要最后将其值保存在数据库中。

How can I make radio buttons to be selected when I click on any of two labels in front of that radio button?

I have following code for radio buttons:

<div class="label_main" style="width:350px">
    <div class="label_radio">
      <input type="radio" name="group1" id="group1" value="0" 
       <?php if($r0==0) { ?>
       checked="checked"
       <?php  } ?>
      />
    </div>
    <div class="label_top">
      <label for="group1">First Option </label>
    </div>
    <div class="label_desc">
      <label for="group1">This is first option </label>
    </div>
  </div>
      </div> 

  <div class="label_main" style="width:350px">
    <div class="label_radio">
      <input type="radio" name="group1" id="group1" value="1"
       <?php if($r0==1) { ?>
       checked="checked"
       <?php  } ?>
      />
    </div>
    <div class="label_top">
      <label for="group1">Second Option </label>
    </div>
    <div class="label_desc">
      <label for="group1">This is Second Option </label>
    </div>
  </div>          

Only problem I am having if I change radio ID it work fine, but I need same id cause I need its value at the end to save in database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2024-08-14 05:20:40

您将 name 属性与 id 属性混淆了。

它们应该具有相同的名称,但具有不同的 id。

例如:

<input type="radio" name="group1" id="group1_0" value="0">
<input type="radio" name="group1" id="group1_1" value="1">

<label for="group1_0">CLicky 0</label>
<label for="group1_1">CLicky 1</label>

You're confusing the name attribute with the id attribute.

They should each have the same name, but a different id.

For example:

<input type="radio" name="group1" id="group1_0" value="0">
<input type="radio" name="group1" id="group1_1" value="1">

<label for="group1_0">CLicky 0</label>
<label for="group1_1">CLicky 1</label>
清醇 2024-08-14 05:20:40

输入的 ID 和名称不必相同。保留名称并将第二个无线电的 ID 更改为其他内容(并将第二个标签的“for”属性更改为该 ID)。
当您将表单提交到服务器时,您将通过“name”属性获得值。服务器不知道任何有关 ID 的信息。

ID and name of the input don't have to be the same. Leave the name and change the ID of the second radio to something else (and change the second label's "for" attribute to that ID).
When you will submit the form to the server you will get the value by the "name" attribute. Server doesn't know anything about IDs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文