如何创建一个带有可点击标签的HTML复选框(这意味着单击标签将复选框打开/关闭)?


当前回答

它也很有效:

<form>
    <label for="male"><input type="checkbox" name="male" id="male" />Male</label><br />
    <label for="female"><input type="checkbox" name="female" id="female" />Female</label>
</form>

其他回答

使用这个

<input type="checkbox" name="checkbox" id="checkbox_id" value="value">
<label for="checkbox_id" id="checkbox_lbl">Text</label>


$("#checkbox_lbl").click(function(){ 
    if($("#checkbox_id").is(':checked'))
        $("#checkbox_id").removAttr('checked');
    else
       $("#checkbox_id").attr('checked');
    });
});

它也很有效:

<form>
    <label for="male"><input type="checkbox" name="male" id="male" />Male</label><br />
    <label for="female"><input type="checkbox" name="female" id="female" />Female</label>
</form>
<label for="myInputID">myLabel</label><input type="checkbox" id="myInputID" name="myInputID />
<label for="vehicle">Type of Vehicle:</label>
<input type="checkbox" id="vehicle" name="vehicle" value="Bike" />

使用label元素和for属性将其与复选框关联:

<label for</label>=“myCheckbox”>某些复选框<input type=“checkbox” id=“myCheckbox” />