2012-06-04 51 views
0

我希望這是我做過的傻事。我有一個靠近底部的函數unigref,這(我相信)輸出一個字符串。但是,當我調用構建jQuery選擇器的函數時,我無法使其正常工作。我知道所有其他的工作,因爲當我使用靜態字符串單選按鈕被選中。爲什麼不能輸出到這個jQuery選擇器?

這是我的jsfiddle/9Edxx。請幫忙。

var checkCount = 0; 
var maxChecks = 2; 

$(document).ready(function() { 

$("#test").click(function() { 
    alert($(':checked').length); 
}); 

$(':checkbox[name=checkbox]').change(function() { 


    checkCount = $(':checkbox:checked').length; 

    if (checkCount >= maxChecks) { 
     $(':checkbox[name=checkbox]').not(':checked').attr('disabled', true); 
     $(":radio[value="+uniqref()+"]").prop('checked', true); 


    } else { 
     $(':checkbox[name=checkbox]:disabled').attr('disabled', false); 
    } 

    if (this.checked) { 
     $("td.label").append("<label>" + this.value + "</label>"); 
    } else { 
     $("td.label").find(":contains('" + this.value + "')").remove(); 
    } 

}); 

$('#button').click(function() { 
    alert(uniqref()); 
}); 


function uniqref() { 
    return $("td.label").text().split('').sort().join('').replace(/\s/g, ""); 
} 


});​ 

UPDATE:錯字一直是正確的,但問題依然存在。

http://jsfiddle.net/9Edxx/

回答

4

http://jsfiddle.net/7mvmT/6/

基本上這行:

$(":radio[value="+uniqref()+"]").prop('checked', true); 

過早稱爲(實際上檢查複選框之前甲簡單,醜陋的黑客:

setTimeout(function(next) {    
    $(":radio[value="+uniqref()+"]").prop('checked', true);  
}, 0); 

解決了。

另外,你有一個像Niko提到的錯字。

+0

謝謝!我花了整個週末來做這個!我會將你標記爲正確的答案。謝謝!! (對尼科感覺不好,有反正我可以欣賞他的輸入嗎?) –

+0

@SajeevShanmuganandarajah不要擔心,你應該選擇爲重大故障提供解決方案的答案;-) – Niko

+0

9 upvotes是不是一個不好的抓住;) – wroniasty

9

沒錯這就是非常愚蠢的:它只是一個錯字。

$(":radio[value="+unigref()+"]").prop('checked', true); 

應該

$(":radio[value="+uniqref()+"]").prop('checked', true); 

用小寫Q而不是G


另外,在實際更新td.label的值之前調用uniqref()。

應該在這個順序:

if (this.checked) {    
    // ... 
} 

if (checkCount >= maxChecks) {    
    // ... 
} 

http://jsfiddle.net/7mvmT/7/

+0

哇,好眼睛:) – wroniasty

+0

我希望是,但可惜校正錯字仍然不解決問題:S –

相關問題