<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<input type="checkbox" name="check" style="display:none;" value="check1" id="ch1">
<label for="ch1">check1</label>
<input type="checkbox" name="check" style="display:none;" value="check2" id="ch2">
<label for="ch2">check2</label>
</body>
</html>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
function showValues() {
alert(this.value);
}
$( "input[type='checkbox']" ).on( "click", showValues );
</script>当您单击“ie10”文本时,它的工作方式类似于下面的图片

但它不适用于ie8
发布于 2014-06-26 03:48:07
将<script>标记移动到<body>中。
我认为您希望注册您的点击标签,而不是复选框(因为您实际上没有显示复选框)。试试这个:
function showValues() {
alert($("#"+$(this).attr("for")).val());
}
$("label").on("click", showValues);发布于 2014-06-26 03:51:18
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(){
function showValues() {
alert(this.value);
}
$( "input[type='checkbox']" ).on( "click", showValues );
});
</script>
</head>
<body>
<input type="checkbox" name="check" style="display:none;" value="check1" id="ch1">
<label for="ch1">check1</label>
<input type="checkbox" name="check" style="display:none;" value="check2" id="ch2">
<label for="ch2">check2</label>
</body>
</html>发布于 2014-06-26 04:40:05
there are several items within the code
that result in 'browser guesses' as to what you actually want.
Like having both check boxes having the same name
they are NOT radio buttons so the names should be unique
A input attribute ID="..." is for CSS definitions, not for input identification
<input type="checkbox" name="check" style="display:none;" value="check1" id="ch1">
<label for="ch1">check1</label>
<input type="checkbox" name="check" style="display:none;" value="check2" id="ch2">
<label for="ch2">check2</label>
To greatly help the browser, the code should look more like:
<label>
check1
<input type="checkbox" name="check1" style="display:none;" value="check1" />
</label>
<label>
check2
<input type="checkbox" name="check2" style="display:none;" value="check2" />
</label>https://stackoverflow.com/questions/24421997
复制相似问题