IE7问题我的jQuery

我有以下代码片段。 基本上我正在尝试做的是在第一次单击function中,我遍历我的缓存JSON数据并显示该id存在的任何值。 在第二个更改函数中,每当其中一个元素更改值时(即“是”或“否”,反之亦然),我会捕获。

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

在Firefox中,一切都按预期工作(当然)。 但是,在IE7中却没有。 在IE7中,如果我选择一个显示来自clickfunction的警报的单选按钮,那么它还会为更改的function添加到数组中。 但是,如果单选按钮没有从点击function执行任何操作,则不会添加arrays以进行更改。

当我看到这段代码时,我想我可以将这两个函数组合在一起,但是现在我只想让它在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  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函数中:

  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  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; } }); }); 

但…

我必须保持改变function相同。 从我的测试中我发现.click函数适用于IE7的单选按钮和复选框元素,但.changefunction适用于IE7和FF中的文本框和文本区以及单选按钮和复选框元素的原始function。

这个真的很乱。 感谢@Patricia的关注。 这里的建议确实引出了我的解决方案。 我打算不回答这个问题,因为我想知道是否有更清洁的解决方案。

事实:单选按钮和复选框上的change事件仅在focus丢失时(即blur事件即将发生时)被触发。 要实现“预期”行为,您真的想要挂钩click事件。

你基本上想要改变

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

 $('input:radio').live('click', functionName); $('input:not(:radio)').live('change', functionName); function functionName() { // Code. } 

(但是我也会使用:checkbox来考虑复选框:checkbox对于表格中有任何forms的情况,您希望将它们视为radiobuttons)

我认为这是因为当检查和无线电丢失焦点时,IE会触发更改。 因此,如果警报弹出,则焦点丢失,因此更改事件正在触发。

编辑:

尝试将$('input')选择器更改$('input:not(:radio)')

因此,点击将触发您的无线电和所有其他人的更改。

编辑#2:

如何将变更中发生的事情变成一个单独的函数。 以索引作为参数。 然后你可以从change()和click()调用该函数。 完成点击后,调用该函数。

你在一个document.ready()函数中声明了你的blnCheck变量(你不需要其中两个,它们都可以在一个中。

这意味着您声明的变量将不是实际调用更改函数时使用的变量,而是您将获得某种隐式全局变量。 不知道这是否属于它的一部分,但可能值得一看。 您应该将此声明放在JS文件的顶部。