checkbox探究

介绍checkbox

checkbox: A check box. You must use the value attribute to define the value submitted by this item. Use the checked attribute to indicate whether this item is selected. You can also use the indeterminate attribute (which can only be set programmatically) to indicate that the checkbox is in an indeterminate state (on most platforms, this draws a horizontal line across the checkbox).


不能用眼睛判断checked

<input type="checkbox"/>

无论我如何点击,f12出来的源代码都没有改变,并不显示checked,这是需要注意的一个地方,我们不能在前台通过f12来判断checked


通过js动态判断checked

<input type="checkbox" id="t"/>
<input type="button"  id="s"/>
<script>
    var s=document.getElementById("s");
    s.onclick=function() {
        console.log(document.getElementById("t").checked);
    }
</script>

可以通过document.getElementById("").checked看输出的true||false来判断


高级用法

<style>
        #bg{
             300px;
            height: 300px;
            background-color: #00cccc;
        }
        #box:checked+#bg{
            background-color: #00ee00;
        }
</style>
<body>
    <input type="checkbox" id="box"/>
    <div id="bg"></div>
</body>

这里的关键点是#box:checked,意思就是我们的checkbox被点击后,虽然前文我们提过f12无法看到checked的改变,但此处的css的checked选择器是可以起作用的。

checkbox被点击后,变成checked,这时+#bg{ background-color: #00ee00; }就起作用了,+选择器指代选择之后的元素,所以此处就是匹配到我们的bg注意:如果这里不用+,而是直接使用空格选择器是不生效的

#box:checked #bg{
    background-color: #00ee00;
}

因为空格选择器匹配的是后代元素,而此刻的bg明显是box的同代元素,所以此处需要使用+或者~

接回原来那里,点击checkbox之后,bg的背景色发生改变,至此,整个简单的效果完成。

效果虽然简单,但同时提供了一种不需要js就可以做到点击改变效果的新思路。


checkbox配合label标签的使用

<label><input type="checkbox" id="cbox1" value="first_checkbox"> This is the first checkbox</label><br>

<input type="checkbox" id="cbox2" value="second_checkbox"> <label for="cbox2">This is the second checkbox</label>



checkbox多选以及传数据

checkbox一般用来做复选框

<form action="">
    你最爱的水果
    <p>
        <input type="checkbox" name="fruits" value="apple"/>苹果<br>
        <input type="checkbox" name="fruits" value="orange"/>橙子<br>
        <input type="checkbox" name="fruits" value="pear"/>梨<br>
        <input type="checkbox" name="fruits" value="watermelon"/>西瓜<br>
    </p>
    <input type="submit" value="提交"/>
</form>
你最爱的水果

苹果
橙子

西瓜

后台会接收到选择的checkbox,数据保存在fruits中,遍历数组获得


扩展:radio的用法

A radio button. You must use the value attribute to define the value submitted by this item. Use the checked attribute to indicate whether this item is selected by default. Radio buttons that have the same value for the name attribute are in the same "radio button group"; only one radio button in a group can be selected at a time.

<FORM ACTION="../cgi-bin/mycgi.pl">
    What size pizza?
    <P>
        <INPUT TYPE=RADIO NAME="pizzasize" VALUE="S">small<BR>
        <INPUT TYPE=RADIO NAME="pizzasize" VALUE="M" CHECKED>medium<BR>
        <INPUT TYPE=RADIO NAME="pizzasize" VALUE="L">large
    </P>
    <INPUT TYPE=SUBMIT VALUE="submit">
</FORM>
What size pizza?

small
medium
large

Note that it is the content of the VALUE attribute that is sent to the CGI.

If no CHECKED attribute is used, different browsers have different ways of displaying the initial state of a series of radio buttons. Netscape and MSIE have none of the buttons selected. Mosaic selects the first button.

也就是说,上述的代码,传给后台的值就是name=pizzasize,而这个name对应的value就是前台页面点击选中的按钮对应的value,我们还可以创建多组radio,如下

<input type="radio" name="group1" value="Milk"> Milk<br>
<input type="radio" name="group1" value="Butter" checked> Butter<br>
<input type="radio" name="group1" value="Cheese"> Cheese
<hr>
<input type="radio" name="group2" value="Water"> Water<br>
<input type="radio" name="group2" value="Beer"> Beer<br>
<input type="radio" name="group2" value="Wine" checked> Wine<br>

Milk

Butter

Cheese


Water
Beer
Wine

radio多选

radio无法做多选,要想实现多选用checkbox


radio的高级用法

radio也是可以像前文的checkbox那样使用的。

#bg{
         300px;
        height: 300px;
        background-color: #00cccc;
}
#dio:checked~#bg{
        background-color: #00ee00;
}


<input type="radio" id="dio"/>
<div id="bg"></div>

注意:radio与checkbox不同,radio选择后,无法再次点击取消选择,除非存在两个radio供交互选择才能取消,这是radio的特性,无法点击原来的radio取消选择。


select介绍

<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat" selected="selected">Fiat</option>
<option value="audi">Audi</option>
</select>

全文完,下文,使用checkbox实现按钮滑动效果。

原文地址:https://www.cnblogs.com/jelly7723/p/5599323.html