如果选中复选框,则返回值为:
/doCheckBox.asp?checkbox-1=true如果未选中复选框,则不会收到任何响应:
/doCheckBox.asp?是否有任何方法可以使用隐藏属性返回未选中的复选框值?
doCheckBox.asp
<%
Option Explicit
Response.Expires = 0
Dim cb1
cb1 = Request("checkbox-1")
Response.Write(cb1)
%>checkBox.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).on("ready", function() {
$('input').checkboxradio();
$('button').button();
$('#button').on('click', function(){
var cb2 = $('#checkbox-2').prop('checked') ? true : false;
var cb1 = $('#checkbox-1').prop('checked') ? true : false;
var cb3 = $('#checkbox-3').prop('checked') ? true : false;
var cb4 = $('#checkbox-4').prop('checked') ? true : false;
var cb5 = $('#checkbox-5').prop('checked') ? true : false;
alert(" " + cb1 + " " + cb2 + " " + cb3 + " " + cb4 + " " + cb5)
submitForm();
});
});
function submitForm() {
$('#form1').attr('action', "doCheckBox.asp").submit( function(){
var cb1 = $('#checkbox-1').prop('checked') ? true : false;
$('#checkbox-1').val(cb1);
});
}
</script>
</head>
<body>
<div class="widget">
<h2>Checkbox</h2>
<form id="form1" method="get">
<fieldset>
<legend>Hotel Ratings: </legend>
<label for="checkbox-1">1 Star</label>
<input type="checkbox" name="checkbox-1" id="checkbox-1">
<label for="checkbox-2">2 Star</label>
<input type="checkbox" name="checkbox-2" id="checkbox-2">
<label for="checkbox-3">3 Star</label>
<input type="checkbox" name="checkbox-3" id="checkbox-3">
<label for="checkbox-4">4 Star</label>
<input type="checkbox" name="checkbox-4" id="checkbox-4">
<label for="checkbox-5">5 Star</label>
<input type="checkbox" name="checkbox-5" id="checkbox-5">
<button id="button" type="submit" onClick="submitForm()">Open Dialog</button>
</fieldset>
</form>
</div>
</body>
</html>发布于 2017-03-07 13:31:09
当您发布表单时,只有已启用的表单元素包含在post中。取消勾选复选框被计算为“未启用”,因此,正如您发现的那样,不会显式传递该值。
您可以通过在复选框后直接添加一个隐藏的输入来解决这一问题,例如:
<input type="checkbox" name="checkbox-1" id="checkbox-1">
<input type="hidden" name="checkbox-1" value="false">它们必须具有相同的name,并且必须按此顺序显示(复选框,然后是隐藏输入)。
当表单被张贴时,如果勾选复选框,它将接受第一个表单元素(复选框值(true)),如果没有勾选,它将返回到隐藏的输入,并接受它的值(false)。
https://stackoverflow.com/questions/42649221
复制相似问题