为什么不能将输出函数输入到这个jQuery选择器中?

Saj*_*jah 0 javascript jquery

我希望这是我做过的傻事.我在底部附近有一个函数unigref,我相信它会输出一个字符串.但是,当我调用该函数来构建一个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, "");
}


});?
Run Code Online (Sandbox Code Playgroud)

更新:错字是正确的,但问题仍然存在.

http://jsfiddle.net/9Edxx/

Nik*_*iko 9

是的,它真的很傻:这只是一个错字.

$(":radio[value="+unigref()+"]").prop('checked', true);
Run Code Online (Sandbox Code Playgroud)

应该

$(":radio[value="+uniqref()+"]").prop('checked', true);
Run Code Online (Sandbox Code Playgroud)

用小写Q而不是小写G.


此外,您在实际更新td.label的值之前调用uniqref().

应按此顺序:

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

if (checkCount >= maxChecks) {            
    // ...
}
Run Code Online (Sandbox Code Playgroud)

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