debug,Jquery函数不起作用

时间:2009-12-28 07:20:23

标签: jquery debugging jquery-selectors

HTML代码:HTML代码是动态创建的。

<li>
    <div class="above">What do I like best?</div>
    <div class="below">
    <label>Answer:(1 words)</label>
    <input id="question6" type="text" size="5"/>
    <label id="sign6"/>
    </div>
    </li>
    <li>
    <div class="above">What city do I like?</div>
    <div class="below">
    <label>Answer:(1 words)</label>
    <input id="question7" type="text" size="5"/>
    <label id="sign7"/>
    </div>

Jquery代码:

function subjectivecheck(id){
        alert(id);
        var cost=(new Date().getTime() - start.getTime())/1000;
        var value=$('#question'+id).val();
        $.post("subjectivecheck.php?",{val:value, qid:id,time:cost, a_id:"<?php echo $announcementid; ?>"},function(xm){

            switch(parseInt(xm)){
                case 4:
                { $htm='Congrats,you have passed the test.';
                    $('#success').css({"color":"green"});
                    $('#success').text($htm);
                return; 
                }
                case 1:
                {
                $htm='V';
            $('#sign'+id).css({"color":"green"});
            $('#sign'+id).text($htm);
            break;  
                }
                case 0:{

                     $htm='X';
            $('#sign'+id).css({"color":"red"});
            $('#sign'+id).text($htm);
            break;
                }
                case 3:{
                    $('#subjectivequestion').text('You have failed at this announcement.');

                    $('#choicequestions').text(" ");
                }
            }

        });

    }
    var ajaxCallTimeoutID = null;
    $('input[id^=question]').keyup(function(ev){
        alert(this.id.substr(8));
    if (ajaxCallTimeoutID != null)
        clearTimeout(ajaxCallTimeoutID);

      ajaxCallTimeoutID = setTimeout(subjectivecheck(id), 1000);

    });

当我在question6

中输入内容时
  

question7

, 函数$('input [id ^ = question]')。keyup(function(ev)},不起作用,没有alert()。这个HTML文件的其他jquery函数工作正常。 有什么想法吗?

2 个答案:

答案 0 :(得分:2)

确保将jQuery脚本包装在$(document).ready()

$(document).ready(function() {
  // your jQuery script here.
});

OR

$(function(){
  // your jQuery script here.
});

答案 1 :(得分:0)

确保将该元素“question7”或“question6”绑定到keyup事件时,它会在页面/ DOM中存在,因为它是动态生成的。你可以在firebug中查看它