我为页面上的复选框编写了以下代码。当页面加载时,我需要在默认情况下检查这两个选项。这将显示查询的结果。现在,当其中一个复选框未选中时,需要提交表单,并显示不同的查询结果。复选框总是被选中,即使我取消了一个复选框。有人能指引我到这里吗?由于
<form action="abc.cfm?show=yes" method="post" name="myform">
<table align="center"><tr>
<td>
<input type="checkbox" checked="checked" name="chkbox" id="chkbox1"> <font size="3+"><strong> Agreement Only</strong> </font>
<input type="hidden" name="chk" id="chk1">
<input type="checkbox" checked="checked" name="chkbox" id="chkbox2"> <font size="3+"><strong>Active Employees</strong> </font>
<input type="hidden" name="chk" id="chk2">
</td>
<td>
<input type="Submit" name="submitnow" value="View now">
</td>
</table>
</form>
<cfif isdefined("form.chk1")>
query 1
<cfelseif isdefined("form.chk2")>
query 2
</cfif>
您可以使用<input type="hidden" value=" (the value of your checkbox here) "/>
存储然后发送检查值。
"保存选定的单选按钮状态"到底是什么意思?
在表单被发送到的页面中,您将通过检查您的post变量知道哪个复选框被选中。如果你的想法是用上面的代码再次显示页面,并选中用户选择的复选框,你可以看看下面的(jQuery 1.6+):
$("#chkboxId").prop('checked', true); //check the checkbox with id chkboxId.
$("#chkboxId").prop('checked', false); //uncheck it.
var states = [false, false, true];
function saveStates()
{
var context = $("#chk1,#chk2,#chk3");
$(context).each(function (index) {
states[index] = $(context[index]).prop("checked");
});
}
我没有测试这个,也许有一个语法错误的地方,但你得到的想法。您需要使用jQuery来应用我的解决方案。