2010-07-08 133 views
0

我有以下的jQuery代碼:jQuery:更改函數沒有被誘導?

$(document).ready(function() { 
      $('.restrictiveOptions input[type!=checkbox], .restrictiveOptions select').change(function() { 
       // check off enabled for that row if anything changed 
       $(this).closest('.fieldRow').find('.restrictiveOptionEnabled').attr('checked','true'); 
       alert('changed'); 
      }); 
     }); 

然而,當我更改輸入或選擇,什麼都不會發生(甚至沒有警報)!

我已檢查$('.restrictiveOptions input[type!=checkbox], .restrictiveOptions select')是否正確(通過使用Firebug控制檯並檢查.length,返回484)。 我也確保沒有語法錯誤。

你覺得我做錯了什麼?謝謝!

編輯:我已更新代碼並將$(self)更改爲$(this),但我仍未看到警報。這裏是樣本HTML(抱歉的格式,我說換行,但它是由PHP生成):

<fieldset class="restrictiveOptions"><legend class="collapsable"> 
<img width="12" height="12" title="Collapse" alt="Collapse" class="toggle" src="branding/default/images/collapse.gif"> Restrictive Options</legend> 
<div style=""> 
<div class="fieldRow"> 
<div class="label"> 
<label>Executive</label></div> 
<div class="fieldWrapper"> 
<div class="cellValue"> 
<select onchange="changeRestrictionValue(&quot;event42_41&quot;)" id="event42_41_restrictionType" name="event42_41_restrictionType"> 
<option selected="selected" value="1">is equal to</option> 
<option value="2">is not equal to</option></select> 
<span id="event42_41_restrictionValue1Wrapper" class="event42_41_restrictionValueWrapper"> 
<select onchange="validateField(this)" name="event42_41_restrictionValue" id="event42_41_restrictionValue"> 
<option value="76">Yes</option> 
<option value="77">No</option></select></span> 
<span style="display: none;" id="event42_41_restrictionValue2Wrapper" class="event42_41_restrictionValueWrapper"> 
<select onchange="validateField(this)" name="event42_41_restrictionValue1" id="event42_41_restrictionValue1"> 
<option value="76">Yes</option> 
<option value="77">No</option></select> and 
<select onchange="validateField(this)" name="event42_41_restrictionValue2" id="event42_41_restrictionValue2"> 
<option value="76">Yes</option> 
<option value="77">No</option></select></span></div> 
<div style="float: right;" class="cellValue"> 
<label for="event42_41_enabled">Enabled?</label> 
<input type="checkbox" style="vertical-align: sub; margin: 2px; padding: 0pt;" class="restrictiveOptionEnabled" title="Check me to enable this restrictive option" name="event42_41_enabled" id="event42_41_enabled" value=""> 
</div></div></div> 
+0

您可以發佈隨附的html嗎? – HurnsMobile 2010-07-08 14:09:32

+0

四百八十四!!!! – Pointy 2010-07-08 14:28:21

+0

@HurnsMobile:我做到了,但解決了。不管怎麼說,多謝拉! @Pointy:它是一種形式的動物=)動態創建代碼塊,並且每個代碼都有這個代碼(實際上我只在示例html中顯示一行,其中可能至少有10個)實際上,讓我們做這個數學計算 每行有4個選擇/輸入,每個塊有11行(在我的測試代碼中,但行數也是動態的),目前有11個塊。 11 * 11 = 121,121 * 4 = 484 =) – Garrett 2010-07-08 14:42:50

回答

2

這是不應該有任何區別除非有一些事情你都沒有提到。不要直接綁定處理程序到所有這些元素,請嘗試設置「實時」處理程序:

$(function() { 
    $('.restrictiveOptions input[type!=checkbox], .restrictiveOptions select') 
    .live('click', function(ev) { 
     $(this).closest('.fieldRow').find('.restrictiveOptionEnabled').attr('checked','true'); 
     alert('changed'); 
    }); 
}); 
+0

,它只是讓它工作!謝謝! – Garrett 2010-07-08 14:38:27

5

相反的self,你需要this,像這樣:

$(document).ready(function() { 
    $('.restrictiveOptions input[type!=checkbox], .restrictiveOptions select').change(function() { 
    $(this).closest('.fieldRow').find('.restrictiveOptionEnabled').attr('checked', true); 
    alert('changed'); 
    }); 
}); 

內的事件像這樣的處理程序,this引用的是有問題的元素,self通常用於掛在某種類型的引用上,但它不是固有的,它是你需要定義的東西。在這種情況下,它試圖做$(undefined),這是不準確你想要什麼:)

+0

我已經做出了改變,但警報仍然沒有顯示=( – Garrett 2010-07-08 14:26:07

+0

@Garrett - 你在控制檯中看到任何錯誤嗎?我更新了更準確的'.attr('checked',true);'但看起來你仍然收到一個錯誤,下面是一個針對你的標記的工作版本:http://jsfiddle.net/UKnmx/問題之外的東西一定是錯誤的? – 2010-07-08 14:34:22

+0

它似乎是'更改'功能,並且切換它'生活'使它的工作。OHHH我認爲這是因爲我生成的文件加載行,以便代碼必須先運行,並沒有找到元素,因爲他們還沒有生成。 非常感謝爲了你的努力,這是我的錯誤 – Garrett 2010-07-08 14:44:33