JavaScript .click在IE 11中不起作用

时间:2017-02-10 21:31:33

标签: javascript jquery html dynamics-crm webresource

我一直在努力让这段代码适用于动态crm的多选项集。我的问题是这个代码在Chrome中毫不费力地工作,完全符合我的要求,但在IE中.click事件无论如何都会返回false,如果选中或取消选中复选框。

所有这些代码都是采用一个选项集并将其转换为多选列表并将值设置为两个字段" l1s_trainingmodulesvalues"保存所选选项的值和" l1s_trainingmodules"保留所选选项的标签。

有人可以看看我做错了什么。



<html><head>
<title></title>
<script src="../WebResources/new_jquery3.1.1.min.js" type="text/javascript"></script>
<script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
<script type="text/javascript">

        // function will be called when web resource is loaded on Form.
        $(document).ready(function () 
        {
            ConvertDropDownToCheckBoxList();
        });

        //Coverts option list to checkbox list.
function ConvertDropDownToCheckBoxList() 
{
    var dropdownOptions = parent.Xrm.Page.getAttribute("l1s_trainingmodulesoptionset").getOptions();
    var selectedValue = parent.Xrm.Page.getAttribute("l1s_trainingmodulesvalues").getValue();

    $(dropdownOptions).each(function (i, e) 
    {
        var rText = $(this)[0].text;
        var rvalue = $(this)[0].value;
        var isChecked = false;
        if (rText != '') 
        {
            if (selectedValue != null && selectedValue.indexOf(rvalue) != -1)
                isChecked = true;

            var checkbox = "<label><input type=\"checkbox\"  name =\"r\">" + rText  + "<br></label>"
            $(checkbox)
                .attr("value", rvalue)
                .attr("checked", isChecked)
                .attr("id", "id" + rvalue)
                .on('click',function () 
                {
                    //To Set Picklist Select Values
                    var selectedOption = parent.Xrm.Page.getAttribute("l1s_trainingmodulesvalues").getValue();
                    alert($(this).is(':checked')); /*** This value always returns false in IE but in Chrome it works perfectly*/**
                    if ($(this).is(':checked')) 
                    {
                        if (selectedOption == null)
                            selectedOption = rvalue+"";
                        else
                            selectedOption = selectedOption + "," + rvalue
                    }
                    else 
                    {
                        var tempSelected = rvalue + ",";
                        if (selectedOption != null) 
                        {
                            if (selectedOption.indexOf(tempSelected) != -1)
                                selectedOption = selectedOption.replace(tempSelected, "");
                            else
                                selectedOption = selectedOption.replace(rvalue, "")
                        }
                    }
                    //alert(rvalue);
                    parent.Xrm.Page.getAttribute("l1s_trainingmodulesvalues").setValue(selectedOption);

                    //To Set Picklist Select Text
                    var selectedYear = parent.Xrm.Page.getAttribute("l1s_trainingmodules").getValue();
                    if ($(this).is(':checked')) 
                    {
                        if (selectedYear == null)
                            selectedYear = rText+"";
                        else
                            selectedYear = selectedYear + "," + rText
                    }
                    else 
                    {
                        var tempSelectedtext = rText + ",";
                        if (selectedYear != null) 
                        {
                            if (selectedYear.indexOf(tempSelectedtext) != -1)
                                selectedYear = selectedYear.replace(tempSelectedtext, "");
                            else
                                selectedYear = selectedYear.replace(rText, "");
                        }
                    }
                    //alert(selectedYear);
                    parent.Xrm.Page.getAttribute("l1s_trainingmodules").setValue(selectedYear);

                })
                .appendTo(checkboxList);
        }
    });
}
</script>  
<meta charset="utf-8">
</head><body style="-ms-word-wrap: break-word;"><form><div id="checkboxList">
</div></form>
</body></html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您的checkbox变量未引用复选框元素。它是一个标签(var checkbox = "<label><input type=\"checkbox\" name =\"r\">" + rText + "<br></label>"),没有检查状态。

click处理程序中,更改

if ($(this).is(':checked'))

if ($(this).find('input').is(':checked'))