jQuery的IE7问题[单击和更改功能] [英] IE7 issues with my jQuery [click and change functions]

查看:73
本文介绍了jQuery的IE7问题[单击和更改功能]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码片段.基本上,我想做的是在第一次单击功能中,我遍历缓存的JSON数据并显示该ID存在的任何值.在第二次更改功能中,我捕获了其中一个元素更改值的时间(即,是,否,反之亦然).

I have the following snippets of code. Basically what I'm trying to do is in the 1st click function I loop through my cached JSON data and display any values that exist for that id. In the 2nd change function I capturing whenever one of the elements changes values (i.e. yes to no and vice versa).

这些元素都是通过我从Web服务接收的JSON数据动态生成的.根据我的理解,这就是为什么我必须使用.live功能.

These elements are all generated dynamically though the JSON data I'm receiving from a webservice. From my understanding that is why I have to use the .live functionality.

在Firefox中,一切正常(当然).但是,在IE7中则没有.在IE7中,如果我选择一个单选按钮以显示单击功能的警报,则它还会添加到更改功能的数组中.但是,如果单选按钮对单击功能没有任何作用,则不会将阵列添加到该更改中.

In Firefox everything works as expected (of course). However, in IE7 it does not. In IE7, if I select a radio button that displays an alert from the click function then it also adds to the array for the changed function. However, if the radio button does not do anything from the click function then the array is not added to for the change.

当我看这段代码时,我在想也许可以将这两个功能组合在一起,现在,我只希望它能在IE7中工作.

As I look at this code I'm thinking that I might be able to combine these 2 functions together however, right now I just want it to work in IE7.

$(document).ready(function () {
    //This function is run whenever a 'radio button' is selected.
    //It then goes into the CPItemMetaInfoList in the cached JSON data
    //($.myglobals) and checks to see if there are currently any 
    //scripts to display.

$("input:radio").live("click", function () {
    var index = parseInt(this.name.split(':')[0]);
    for (i = 0; i <= $.myglobals.result.length - 1; i++) {
        if ($.myglobals.result[i].CPItemMetaInfoList.length > 0) {
           for (j = 0; j <= $.myglobals.result[i].CPItemMetaInfoList.length - 1; j++) {
                if (index == $.myglobals.result[i].QuestionId) {
                    alert($.myglobals.result[i].CPItemMetaInfoList[j].KeyStringValue);
                    return;
                }
            }
        }
    }
});
});

$(document).ready(function () {
    var blnCheck = false;
    //Checks to see if values have changed.
    //If a value has been changed then the isDirty array gets populated.
    //This array is used when the questionSubmit button is clickeds
    $('input').live('change', function () {
        blnCheck = false;

        for (i = 0; i <= isDirty.length - 1; i++) {
            if (isDirty[i] == $(this).attr("name")) {
                blnCheck = true;
                break
            }
        }


    if (blnCheck == false) {
        isDirty[arrayCount] = $(this).attr("name");
        arrayCount += 1;
        alert($(this).attr("name"));
    }
});

$('textarea').live('change', function () {
    blnCheck = false;
    for (i = 0; i <= isDirty.length - 1; i++) {
        if (isDirty[i] == $(this).attr("id")) {
            blnCheck = true;
            break
        }
    }

    if (blnCheck == false) {
        isDirty[arrayCount] = $(this).attr("id");
        arrayCount += 1;
        //alert($(this).attr("name"));
    }
});
});

更新:

我不得不将这段代码移入click函数:

UPDATE:

I had to move this chunk of code into the click function:

   blnCheck = false;
   for (i = 0; i <= isDirty.length - 1; i++) {
        if (isDirty[i] == $(this).attr("name")) {
            blnCheck = true;
            break
        }
    }


    if (blnCheck == false) {
        isDirty[arrayCount] = $(this).attr("name");
        arrayCount += 1;
        alert($(this).attr("name"));
    }

赞:

     $(document).ready(function () {
     //This function is run whenever a 'radio button' is selected.
     //It then goes into the CPItemMetaInfoList in the cached JSON data
     //($.myglobals) and checks to see if there are currently any 
     //scripts to display.

     $("input:radio").live("click", function () {
         var index = parseInt(this.name.split(':')[0]);
         for (i = 0; i <= $.myglobals.result.length - 1; i++) {
             if ($.myglobals.result[i].CPItemMetaInfoList.length > 0) {
                for (j = 0; j <= $.myglobals.result[i].CPItemMetaInfoList.length - 1; j++) {
                     if (index == $.myglobals.result[i].QuestionId) {
                         alert($.myglobals.result[i].CPItemMetaInfoList[j].KeyStringValue);
                         return;
                     }
                 }
             }
         }
         blnCheck = false;

         for (i = 0; i <= isDirty.length - 1; i++) {
             if (isDirty[i] == $(this).attr("name")) {
                 blnCheck = true;
                 break
             }
         }

      if (blnCheck == false) {
          isDirty[arrayCount] = $(this).attr("name");
          arrayCount += 1;
      }

     });
});

但是...

我必须保持更改功能不变.通过测试,我发现.click函数适用于IE7的单选按钮和复选框元素,但是.change功能适用于IE7和FF中的文本框和文本区域以及单选按钮和复选框元素的原始功能.

I had to leave the change function the same. From my testing I found that the .click function worked for IE7 for the radio buttons and checkbox elements, but the .change functionality worked for the textboxes and textareas in IE7 and FF as well as the original functionality of the radio buttons and checkbox elements.

这真是一团糟.感谢@Patricia的帮助.这里的建议确实使我找到了这个解决方案.我将不愿回答这个问题,因为我想知道是否还有一种更清洁的解决方案.

This one got real messy. Thanks to @Patricia for looking at it. Here suggestions did lead me to this solution. I'm going to leave the question unanswered as I wonder if there isn't a cleaner solution to this.

推荐答案

事实:仅当focus丢失时(即,当blur事件即将发生时),单选按钮和复选框上的change事件才会被触发).为了实现预期"行为,您确实想钩住click事件.

Fact: change event on radio buttons and checkboxes only get fired when the focus is lost (i.e. when the blur event is about to occur). To achieve the "expected" behaviour, you really want to hook on the click event instead.

您基本上想更改

$('input').live('change', function() {
    // Code.
});

$('input:radio').live('click', functionName);
$('input:not(:radio)').live('change', functionName);

function functionName() {
    // Code.
}

(不过,我也会使用 :checkbox 选择器(如果您有任何形式的表单,则希望将它们等同地视为单选按钮)

这篇关于jQuery的IE7问题[单击和更改功能]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆