doujupa7567 2012-05-04 15:49
浏览 64
已采纳

Javascript - 在页面加载期间和仅在页面加载期间发生的Onclick事件

I'm still fairly new to javascript and I'm not finding where I'm making my mistake. The basic set up I'm working with is a set of radio buttons along with a set of check boxes. Depending on the radio button picked only a specific set of check boxes should be available the rest should be disabled/grayed out. Which check boxes are allowed for a given radio button are passed in through a php array. In my code the exclusive choices refer to the radio buttons while the extended choices refer to check boxes. My javascript and php for this functionality is as follows:

window.onload = function(){
    <?php
    for($i = 0; $i < count($students_information); $i++){
        foreach($exclusive_extended_array as $exclusive => $extended){
            echo "$('".$i."_exclusive_".$exclusive."').onclick = allow(".$i.",".json_encode($extended).");";
        }
    }
    ?>
}
function allow(i, extended){
    $('[id^="'+i+'Extended"]').disabled = true;
    for (var j = 0; j < extended.length; j++) {
        alert(extended[j]);
        $(i+"Extended"+extended[j]).disabled = false;
    }
}

On the loaded page it appears as:

<script type="text/javascript">
window.onload = function(){
    $('0_exclusive_2176').onclick = allow(0,[1975]);
    $('0_exclusive_911').onclick = allow(0,[]);
    $('0_exclusive_795').onclick = allow(0,[1973,1975]);
}
function allow(i, extended){
    $('[id^="'+i+'Extended"]').disabled = true;
    for (var j = 0; j < extended.length; j++) {
        alert(extended[j]);
        $(i+"Extended"+extended[j]).disabled = false;
    }
}
</script>

Unfortunately, what the code ends up doing is running the script when the page loads rather than when one of the radio buttons is checked. I don't think it's an issue with the names of the elements (though I do realize the naming style is not consistent, however, I don't have full control over that). I'm assuming it's just me making a simple mistake in the code and I still don't have enough experience with javascript to catch it yet. Anyone see what I'm doing wrong?

Thank you for your time!

  • 写回答

1条回答 默认 最新

  • douyi0902 2012-05-04 15:54
    关注

    You're assigning your click handlers like this:

    $('0_exclusive_2176').onclick = allow(0,[1975]);
    

    What's happening is: allow is ran and its return value (undefined) is set as the event. You need to set onclick to a function:

    $('0_exclusive_2176').onclick = function(){
        allow(0,[1975]);
    };
    

    You can also make allow return a function:

    function allow(i, extended){
        return function(){
            $('[id^="'+i+'Extended"]').disabled = true;
            for (var j = 0; j < extended.length; j++) {
                alert(extended[j]);
                $(i+"Extended"+extended[j]).disabled = false;
            }
        };
    }
    

    Now $('0_exclusive_2176').onclick = allow(0,[1975]); should work as expected.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解