开发者

debug, Jquery function does not work

HTML code: The HTML code is dynamically created.

<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 code:

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);

    });

When I input something in question6 or

question7

, the function $('input[id^=question]').keyup(function(ev) },does not work, there is no alert() .Other jquery functions of this HTML file works fine. Any idea?


make sure you wrap your jQuery script in $(document).ready().

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

OR

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


Be sure that when you are binding that element "question7" or "question6" to keyup event , It exists in the page/DOM as you are generating it dynamically. You can check it in firebug

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜