【问题标题】:On pageload default a radio button在页面加载默认单选按钮
【发布时间】:2013-04-03 01:04:43
【问题描述】:

我有以下代码用于页面上的复选框。我需要在页面加载时默认检查它们。这将显示查询的结果。现在,当取消选中其中一个复选框时,需要提交表单并需要显示不同的查询结果。即使我取消选中一个框,复选框也总是被选中。有人可以在这里指导我吗?谢谢

   <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>
       &nbsp;&nbsp;<input type="hidden" name="chk" id="chk1">
        <input type="checkbox" checked="checked" name="chkbox" id="chkbox2"> <font size="3+"><strong>Active          Employees</strong> </font>
     &nbsp;&nbsp;<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>

【问题讨论】:

  • 你说需要保存选中的单选按钮状态是什么意思?您需要服务器端的状态还是客户端的状态?
  • 在客户端...

标签: javascript html checkbox


【解决方案1】:

您可以使用&lt;input type="hidden" value=" (the value of your checkbox here) "/&gt; 来存储然后发送检查的值。

“保存选定的单选按钮状态”到底是什么意思?

在您的表单发布到的页面中,您将通过检查您的发布变量来知道选中了哪个复选框。如果您的想法是再次显示带有上述代码的页面,并选中用户选择的复选框,您可以查看以下内容(jQuery 1.6+):

$("#chkboxId").prop('checked', true); //check the checkbox with id chkboxId.
$("#chkboxId").prop('checked', false); //uncheck it.

【讨论】:

    【解决方案2】:
    var states = [false, false, true];
    
    function saveStates()
    {
        var context = $("#chk1,#chk2,#chk3");
        $(context).each(function (index) {
            states[index] = $(context[index]).prop("checked");
        });
    }
    

    我没有对此进行测试,也许某处存在语法错误,但您明白了。您需要使用 jQuery 来应用我的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2015-01-18
      • 2022-09-23
      • 1970-01-01
      相关资源
      最近更新 更多