单选按钮中实现点击文字选中

单选按钮点击相关文字选中

情景:在一个HTML的页面中设计一个表单,要求需要有单选框,并且通过点击单选框后面的相关文字既可以得到选中。

主要两种方法实现,方法1:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>FIRST PAGE</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <style>
         h2{text-align:center}
    </style>
  </head>

  <body>
  <form action="">
  <h2>选择</h2><hr>
      您最喜欢水果?<br>
      <input type ="radio" name = "fruit" value ="" checked>苹果<br>
      <label><input type ="radio" name = "fruit" value ="">桃子</label><br>
      <label><input type ="radio" name = "fruit" value ="">香蕉</label><br>
      <label><input type ="radio" name = "fruit" value ="">梨</label><br>
      <label><input type ="radio" name = "fruit" value ="">其他</label>
  </form>
  </body>
</html>

在上述的代码中

1.有“苹果”的一项没有标签<label>所以必须通过点击前面的单选按钮选择,后面的选项直接点击相关的文字即可。

2.因为在input标签中添加了“checked”,所以当页面加载完之后“苹果”选项是被选中的(即默认选中)。如果想要用JavaScript语句实现可以在改代码后面添加如下的语句:

<script>document.getElementsByName("fruit")[0].checked="checked";</script>//默认的选中第一个选项

方法2:

<label for="apple">苹果</label>
      <input type = "radio" id = "apple" name = "fruit"><br>
      <label for="peach">桃子</label>
      <input type = "radio" id = "peach" name = "fruit"><br>
      <label for="banana">香蕉</label>
      <input type = "radio" id = "banana" name = "fruit"><br>
      <label for="pear">梨</label>
      <input type = "radio" id = "pear" name = "fruit"><br>
      <label for="other">其他</label>
      <input type = "radio" id = "pear" name = "fruit"><br>

在方法1的基础上实现。可见两者之间的简易程度不一。

参考:http://www.cnblogs.com/kingwell/archive/2012/09/28/2707132.html

   http://www.divcss5.com/html/h490.shtml

时间: 2024-07-29 13:29:16

单选按钮中实现点击文字选中的相关文章

点击文字选中单选框或复选框

在选择单选框或者复选框的时候,点击圆点或者方框不太好点,就有了点击文字选中的需求,之前一直想的是用js实现,后来发现一个比较方便的方法. 就是把选项和文字外加label标签,如: <label> <input type="radio" value="A" name="1">A.选项A </label> <label> <input type="radio" value=&q

点击文字选中单选按钮

<tr> <td align="right"> <font color="red">*</font>项目状态: </td> <td align="left"colspan=3"> <c:forEach items="${taglist}" var="l" varStatus="vt"> <

点击文字选中radio

<html><body><form action="" name="form1" method="post"> <br /><label style="cursor:pointer" for="man"><input type="radio" value="http://fanjf.iteye.com/blog/

点击文字使RadioButton被选中

<html><head><title>标题页</title></head><body><br><input type="radio" name="myLike" value="Like1" id="Like1"><label for="Like1"> 体育运动:NBA</label> &l

(九)android中通过selector设置TextView的文字选中和正常时候的颜色

通过selector设置TextView的文字选中和正常时候的颜色的步骤如下所示: 1.在res下面建个文件夹color,color文件夹下面建个tv_text_color_selector.xml,tv_text_color_selector.xml中的代码如下所示: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.androi

CSS 禁止文字选中

现象:html中可能有些地方不想让用户复制文字,或是用a标签做了个点击按钮,点快的时候文字会被选中,很丑,这个时候可以使用下面的方案禁止文字选中. 原因:鼠标点快了. 解决方案: 不同的浏览器设置的内容不一样,user-select不是一个W3C的标准,浏览器的支持不完成,需要对不同的浏览器进行调整. body{ -moz-user-select: none; /*火狐*/ -webkit-user-select: none; /*webkit浏览器*/ -ms-user-select: non

pdf中如何更改pdf文字的颜色

pdf中如何更改文字的颜色 有时候真的不知道一些PDF文档的制作者的设计思路是怎么想出来的,把背景弄的过于阴暗或者把文字本身调的颜色非常暗,特别影响我们的阅读.但是很多人在下载完这些令人头疼的PDF之后就不知道怎么办了,只能默默去读这份文字发暗的文章.我们可以自己动手把PDF文章中的文字改的亮一点,这样我们就能非常轻松愉快的进行阅读了.可是最近我下载了一部小说,里面有大段的文字都是灰色的,非常影响我的阅读体验. 这本小说还蛮好看的,而且资源很难找,PDF中的文字是只读性质的,如何去修改呢?不用担

php实现点击文字提交表单并传递数据至下一个页面

<?php $id="4";//等会要把这个数据传到第二个页面 ?> <?php echo "<li>"; echo "<form name=\"myquestion\" method=\"post\" action=\"question.php\">"; echo "<input name=\"questionID\&

ListView item中按钮点击实现删除

ListView item中按钮点击实现删除 在处理ListView 中item的按钮点击事件首先需要在item的根布局中添加 android:descendantFocusability="blocksDescendants"属性,否则item的点击事件会把其中image button的点击事件屏蔽掉 item的布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"