简体   繁体   English

如果未选中复选框,则仅允许触发事件

[英]Only allow to trigger event if checkbox is not checked

I have a table with a checkbox, when the user clicks on the checkbox, something is copied into a div, so the user knows what he has selected. 我有一个带有复选框的表,当用户单击复选框时,会将某些内容复制到div中,以便用户知道他所选择的内容。

However, if I uncheck and check again, then its copied again to the div. 但是,如果我取消选中并再次检查,则将其再次复制到div。

           //On every checkbow that is clicked
    var flag = false;
    $("#ctl00_PlaceHolderMain_myGrid input").change(function () {
        if (this.checked && flag === false) {
            var jobCode = $(this).parent().parent().parent().find("td:eq(2)").text()
            var jobName = $(this).parent().parent().parent().find("td:eq(1)").text()
            var displayvalue = jobCode.toUpperCase() + " - " + jobName.toUpperCase();
            //var removeDiv = $("#" + clientCode);

            //removeDiv.remove();         

            AddSelectedJob(jobCode, displayvalue);
            FillSelectedJobs();            
        }
    });

//Add selected job in the results div
function AddSelectedJob(id, display) {
    //create a div for every selected job
    $("[id$=ResultsDiv]").append('<div class="selectedjobs" id=' + id + '>' + display + '<a href="javascript:;" onclick="removeSelectedJob(this)">Remove selected job</a></div>');
}

//Removes the selected job from the resutls div
function removeSelectedJob(el) {
    $(el).parent().remove();
}

    function FillSelectedJobs() {

        //save values into the hidden field
        var selectedJobs = $("[id$=ResultsDiv]").find("[class$='selectedjobs']");
        var returnvalue = "";

        for (var i = 0; i < selectedJobs.length; i++)
            returnvalue += selectedJobs[i].id + ";";

        $("[id$=HiddenClientCode]").val(returnvalue);

    }

This is the generated html 这是生成的html

<div>
            <div style="height: 300px; overflow: auto; float: left">
                <div>
        <table cellspacing="0" cellpadding="4" id="ctl00_PlaceHolderMain_myGrid" style="color:#333333;width:100%;border-collapse:collapse;">
            <tr style="color:White;background-color:#5D7B9D;font-weight:bold;">
                <th scope="col">&nbsp;</th><th scope="col">JobCode</th><th scope="col">JobName</th><th scope="col">JobPartner</th><th scope="col">JobManager</th><th scope="col">ClientName</th>
            </tr><tr style="color:#333333;background-color:#F7F6F3;">
                <td>
                                <input id="ctl00_PlaceHolderMain_myGrid_ctl02_CheckBox1" type="checkbox" name="ctl00$PlaceHolderMain$myGrid$ctl02$CheckBox1" />
                            </td><td>column1</td><td>column2</td><td>column3</td><td>column4</td><td>column5</td>
            </tr>
        </table>
    </div>
            </div>
            <div style="margin-top: 0px; margin-left: 10px; float: left">
                <span>Selected :</span>
                <div id="ResultsDiv" style="margin-top: 0px">
                </div>
            </div>

Update 1: A more clear explanation: 更新1:更清晰的解释:

I have a table with checkboxes, when a row is selected the job code and job name is copied to a div called results. 我有一个带有复选框的表,当选择一行时,作业代码和作业名称将复制到称为结果的div中。 In that div, I created an anchor tag to remove the div itselft. 在该div中,我创建了一个锚标记来删除div本身。 When the remove selected job link is clicked, I should be able to add it again from the table when click the checkbox again. 单击“删除选定的作业”链接后,再次单击该复选框,我应该能够从表中再次添加它。 However if the selected job is already on the div results, I should not be able to add it again. 但是,如果所选作业已经在div结果中,则我应该不能再次添加它。 Please see question here also: Remove div on the click of a link that was created dynamically 也请在此处查看问题: 单击动态创建的链接后删除div

Why not just use the checked attribute instead of unbinding the event 为什么不只使用checked属性而不是取消绑定事件

var flag = false;
$("#ctl00_PlaceHolderMain_myGrid input").change(function () {
    if(this.checked && flag === false) {
         flag = true;  // set it to true so that it does 
                       // fire this block again
         // do something
    }
    else {
        // do something else
    }
});

OR 要么

For a cleaner approach, if you want the event to fire only once for the life time of the page load , then just unbind the event 对于更干净的方法,如果您希望事件在页面加载的生命周期内仅触发一次,则只需取消绑定该事件

$(this).off('change')   // If using .on

EDIT 编辑

If the above code does not help this should work 如果上面的代码没有帮助,这应该工作

if (this.checked) {
   $(this).unbind('change');   // or //   $(this).off('change');
   AddSelectedJob(clientCode, displayvalue);
   FillSelectedJobs();
}

AddSelectedJob之前,您可以检查特定div的内容是否为空。

Do not do in this way. 不要以这种方式做。 What I would do. 我会怎么做。 On every checkbox click, I will get the list of checked checkbox again and display it in a result div. 每次单击复选框时,我都会再次获得已选中复选框的列表,并将其显示在结果div中。

In your case you are appending DIV on every click. 在您的情况下,您将在每次点击后附加DIV。 Which will cause duplication if I checked multiple times. 如果我多次检查,将导致重复。

Still if you want to stick to this pattern then you have to remove entry from the ResultDiv when you unchecked the checked checkbox. 仍然,如果您要坚持这种模式,那么当您取消选中选中的复选框时,必须从ResultDiv中删除条目。

Please let me know if you have any query or confusion. 如果您有任何疑问或困惑,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM