【问题标题】:How to show Javascript confirm box when checkbox is unchecked and then if user selects cancel, leave the checkbox checked?如何在未选中复选框时显示Javascript确认框,然后如果用户选择取消,则选中复选框?
【发布时间】:2013-01-08 04:02:45
【问题描述】:

我有一个小的html form,带有三个复选框,每个row 一个。我希望在用户取消选中该框时显示javascript confirm box,然后如果用户选择取消,checkbox 仍处于选中状态。我已经搜索了这个站点和其他一些站点,但几乎所有内容都与检查 checkbox 有关,例如他们是否必须同意某些条款或其他内容。我对Javascript 很陌生,但是我尝试根据我的外观创建一个函数,但它不起作用。

这是我的表格:

<form action="" method="post">
  <table id="table">
    <tr>
      <td>Checkbox One</td>
      <td align="center">
        <input type="checkbox" name="check1" checked="checked" onchange="cTrig(this.name)"></input>
      </td>
    </tr>
    <tr>
      <td>Checkbox Two</td>
      <td align="center">
        <input type="checkbox" name="check2" checked="checked" onchange="cTrig(this.name)"></input>
      </td>
    </tr>
    <tr>
      <td>Checkbox Three</td>
      <td align="center">
        <input type="checkbox" name="check3" checked="checked" onchange="cTrig(this.name)"></input>
      </td>
    </tr>
    <tr align="center">
      <td colspan="2">
        <input type="submit" name="submit" value="submit"></input>
      </td>
    </tr>
  </table>
</form>

这是我尝试过的功能,但不起作用:

function cTrig(name) {
  if (name.checked == true) {
    confirm("Are you sure you want to do this?");
    return true;
  } else {
    return false;
  }
}

这是jsfiddle

我更喜欢Javascript 中的内容,因为我想在进入jquery 之前更熟悉该语言,但如果必须在jquery 中完成,那很好。干杯。

【问题讨论】:

    标签: javascript html checkbox


    【解决方案1】:

    试试这个方法

    <form action="" method="post">
      <table id="table">
        <tr>
          <td>Checkbox One</td>
          <td align="center">
            <input type="checkbox" name ="check1" id="check1" checked='checked' onchange="cTrig('check1')"></input>
          </td>
        </tr>
        <tr>
          <td>Checkbox Two</td>
          <td align="center">
            <input type="checkbox" name="check2" id="check2" checked='checked' onchange="cTrig('check2')"></input>
          </td>
        </tr>
        <tr>
          <td>Checkbox Three</td>
          <td align="center">
            <input type="checkbox" id="check3" name="check3" checked='checked' onchange="cTrig('check3')"></input>
          </td>
        </tr>
        <tr align="center">
          <td colspan="2">
            <input type="submit" name="submit" value="submit"></input>
          </td>
        </tr>
      </table>
    </form>
    

    通过使用元素 ID

    function cTrig(clickedid) { 
          if (document.getElementById(clickedid).checked == true) {
            return false;
          } else {
           var box= confirm("Are you sure you want to do this?");
            if (box==true)
                return true;
            else
               document.getElementById(clickedid).checked = true;
    
          }
        }
    

    Working Demo

    使用名称

    function cTrig(clickedid) { 
          if (document.getElementsByName(clickedid)[0].checked == true) {
            return false;
          } else {
           var box= confirm("Are you sure you want to do this?");
            if (box==true)
                return true;
            else
               document.getElementsByName(clickedid)[0].checked = true;
    
          }
        }
    

    Demo Using element name

    【讨论】:

    • 复选框应该分配id属性。
    • @NewDeveloper 是的,我在演示链接中有那个,无论如何我已经在这里发布了代码......感谢您指出这一点
    • 这太完美了!非常感谢你!我希望我可以为您的彻底性和两个不同的答案给出 +2,两者都按要求使用 Javascript。谢谢谢谢!!
    • @AndrewFox 很高兴我能帮助你
    【解决方案2】:

    我会这样做:

    您的表格:

    <form action="" method="post">
      <table id="table">
        <tr>
          <td>Checkbox One</td>
          <td align="center">
            <input type="checkbox" name="check1" checked="checked" onchange="cTrig(this)"></input>
          </td>
        </tr>
        <tr>
          <td>Checkbox Two</td>
          <td align="center">
            <input type="checkbox" name="check2" checked="checked" onchange="cTrig(this)"></input>
          </td>
        </tr>
        <tr>
          <td>Checkbox Three</td>
          <td align="center">
            <input type="checkbox" name="check3" checked="checked" onchange="cTrig(this)"></input>
          </td>
        </tr>
        <tr align="center">
          <td colspan="2">
            <input type="submit" name="submit" value="submit"></input>
          </td>
        </tr>
      </table>
    </form>
    

    javascript:

    function cTrig(box) {
      if (!box.checked) {
        if (!confirm("Are you sure you want to do this?")) {
            box.checked = true;
        }
      }
    }
    

    【讨论】:

    • 我将如何修改它以便显示带有确认按钮的 div?
    【解决方案3】:

    使用 jQuery(因为它更好),但如果需要我可以不用它:

    $('#check1').change(function(){
        if($("#check1").prop('checked') == false)
        {
            var i = window.confirm("Sure?");
            if(i == true)
            {
                $("#check1").prop('checked', false);
            }
            else
            {
                $("#check1").prop('checked', true);
            }
        }
    });
    

    这还假设您为每个复选框分配了一个 id,这是您需要的。在这种情况下,名称不会削减它。

    <input type="checkbox" id="check1" name="check1" checked="checked"></input>
    

    等等……

    http://jsfiddle.net/RbENV/10/

    请注意,我采用原生方式进行确认对话,但使用 jQuery UI 您可以更多地控制该框。

    编辑:添加了一个检查以确保它仅在您取消选中它时发生。错过了那部分。 (请注意检查是否为 false,因为当更改事件触发时,更改已经发生)

    【讨论】:

    • 感谢您的回复。虽然效果很好,但我还是选择了@Sibu 答案,因为它是用 Javascript 编写的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 2015-12-20
    • 1970-01-01
    相关资源
    最近更新 更多