【问题标题】:Terms and Con checkbox is not working条款和条件复选框不起作用
【发布时间】:2015-12-06 21:03:48
【问题描述】:

我实际上在这里找到了一个 js 代码,但它似乎不适合我。有人可以检查代码吗?也许我犯了一些错误。我正在寻找在进入新页面之前添加复选框的可能性。非常感谢 PS 我刚开始,所以请善待^^

   <script>
        function agree(){
            b = document.getElementById('btn');
            c = document.getElementById('checkbox');
             if(c.checked == true){

             input type=\"button\" name=\"Button\" value=\"Submit\" onClick =          \"document.location.href='page2.htm' \";

             }else{

             b.innerHTML = "<input type=\"button\" name=\"Button\" value=\"Submit\" disabled=\"disabled\"/>"

             }
        }
    </script>

    </head>
    <body>
    <table width="800" height="50" border="0">
    <tr>
    <td width="500" height="42"> <input type="checkbox" name="checkbox"     value="checkbox"  onclick="agree()"/>
      <label> I agree to the Terms and Conditions. <br>
    </label></td>
    <td width="800" align="left">

    <div id="btn">
     <input type="button" name="Button" value="Submit"  disabled="disabled"/>
    </div>

    </td>
     </tr>
   </table>

【问题讨论】:

  • 您没有任何元素的 ID 为按钮或复选框,因此 document.getElementById 将失败。
  • 定义“不工作”。它会崩溃吗?它显示错误的东西吗?有 JS 调试器给你任何细节吗?

标签: html


【解决方案1】:

直接&完整代码here

我更改了您的脚本以简化它。让我们看看我是怎么做到的:

function agree() {
    //Go and find the button.
    b = document.getElementById('btn');
    //Go and find the checkbox
    c = document.getElementById('checkbox');

    //Set the buttons disabled state to the inverse of the checkbox:
    //Checkbox == true ==> Button = enabled.
    //Checkbox == false ==> Button = disabled.
    b.disabled = !c.checked;
}

现在,最后一个重要部分:将 id 设置为正确的元素:

<table width="800" height="50" border="0">
    <tr>
        <td width="500" height="42"> 
            <input type="checkbox" name="checkbox" id="checkbox" value="checkbox"  onClick="agree();"/>
            <label> I agree to the Terms and Conditions. <br></label>
        </td>
        <td width="800" align="left">
            <div>
                <input id="btn" type="button" name="Button" value="Submit"  disabled="disabled"/>
            </div>
        </td>
    </tr>
</table>

按钮本身的 ID 为 btn,而复选框的 ID 为 checkbox

【讨论】:

  • 非常感谢。似乎可以工作,但我在哪里可以在按钮后面添加目标 URL? “点击提交,你会被转发到页面 xyz?”
  • @TitosTarantula 看看here 这个问题。 :) - 另外:如果您觉得我的回答有帮助,请点赞并将其标记为“答案”。
  • 再好一点:D 是否也可以让按钮先变灰,然后当复选框被激活时变成另一种颜色?我在哪里可以给你点赞并标记它?
  • 我的答案旁边有一个上下箭头,您可以单击“向上”箭头,然后单击箭头下方的“V”将其标记为答案。 - 对于您的背景颜色问题,have a look here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-18
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
相关资源
最近更新 更多