简体   繁体   English

如何在php中使复选框旁边的文本可点击

[英]How can I make the text adjacent to my checkbox clickable in php

how can I make users check on textclick? 如何让用户检查textclick? My script is: 我的脚本是:

 <?php 
      foreach($getcountry_name as $key => $value)
 { ?>
    <tr>
        <td width="25" valign="top">
        <input name="country_name[]"  
             type="checkbox" checked="checked" 
             value="<?php echo $value['country_id']; ?>" />
        </td>
        <td>
        <label class="type01"><?php echo $value['country_name']; ?></label>
        </td>
    </tr> 
   <?php
     }
   ?>        

thanks in advance 提前致谢

The <label> needs a for attribute that contains the ID of the <input> field it's linked to. <label>需要一个for属性,该属性包含链接到的<input>字段的ID。

eg 例如

<input id="myID" name="country_name[]"  type="checkbox" checked="checked" value="<?php echo $value['country_id']; ?>" />
<label for="myID" class="type01"><?php echo $value['country_name']; ?></label>

Edit: 编辑:

Applying it to your example, 将其应用于您的示例,

<?php 
      foreach($getcountry_name as $key => $value)
 { ?>
    <tr>
        <td width="25" valign="top">
        <input id="country_name_<?php echo $value['country_id']; ?>" name="country_name[]"  
             type="checkbox" checked="checked" 
             value="<?php echo $value['country_id']; ?>" />
        </td>
        <td>
        <label for="country_name_<?php echo $value['country_id']; ?>" class="type01"><?php echo $value['country_name']; ?></label>
        </td>
    </tr> 
   <?php
     }
   ?>  

Give the input an ID and let your label use this as the value for a for attribute. input一个ID,然后让您的label将其用作for属性的值。


<tr>
    <td width="25" valign="top">
        <input id="<?php $id=uniqid('id_'); echo $id; ?>" name="country_name[]" type="checkbox" checked="checked" value="<?php echo $value['country_id']; ?>" />
    </td>
    <td>
        <label for="<?php echo $id; ?>" class="type01"><?php echo $value['country_name']; ?></label>
    </td>
</tr>

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

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