【问题标题】:uncheck the checkbox that was just checked based on javascript function取消选中刚刚基于javascript函数选中的复选框
【发布时间】:2011-11-04 21:16:36
【问题描述】:

我已经到处寻找答案,希望这里有人可以提供帮助。当您从一组复选框中选中一个复选框时,我有一个脚本会运行。复选框被分配一个值“客户ID”

<input type="checkbox" name="inv_add[]" onclick="setcid(\''.$result['cid'].'\')" value='.$result['id'].'/>

javascript 的重点是为隐藏字段分配一个值,告诉下一页所有复选框的客户 ID 是什么。

如果您的用户选中了一个分配给不同客户的框(因此不属于该组),我想提醒用户,然后取消选中最后一个选中的框。 我可以一直到警报,但不能取消选中用户刚刚选中的框。

function setcid(cid) {

if (window.set_x === undefined) {
    sethidden = document.getElementById('cid');
    sethidden.value = cid;
    set_x = cid;
    alert ("finished setting x");

    }

    else if (cid !== set_x){
            alert ("You are trying to add two different companies to the same invoice");
                           /*Uncheck the box just checked by user*/
            }

        else {
            alert("they are the same");
                           /*no modification required*/
            }

}

【问题讨论】:

    标签: javascript function checkbox


    【解决方案1】:

    您可以在调用函数时传递this 对象:

    http://jsfiddle.net/msV24/

    HTML

    <input type="checkbox" name="inv_add[]" onclick="setcid(\''.$result['cid'].'\',this)" value='.$result['id'].'/>
    

    javascript

    function setcid(cid, sender) {
    
    ...
    
        else if (cid !== set_x){
            alert ("You are trying to add two different companies to the same invoice");
                           sender.checked = false;
            }
    
        else {
            alert("they are the same");
                           /*no modification required*/
            }
    
    }
    

    【讨论】:

      【解决方案2】:

      我将分配您的复选框和chk_[TheIdOfTheHiddleField] 的 ID:

      <input type="checkbox"
             id='.$result['id'].'
             name="inv_add[]"
             onclick="setcid(\''.$result['cid'].'\')"
             value='.$result['id'].' />
      

      然后像这样取消选中它:

      document.getElementById("chk_" + cid).checked = false;
      

      【讨论】:

        猜你喜欢
        • 2013-12-07
        • 1970-01-01
        • 2018-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-18
        • 1970-01-01
        相关资源
        最近更新 更多