views:

42

answers:

2

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?

+1  A: 

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

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

OR

$(function(){
  // your jQuery script here.
});
Anwar Chandra
It doesn't work even I wrap the Jquery script in $(document).ready().
Steven
What can interfere the execution of this Jquery code?
Steven
A: 

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

Tinku
Yes it exists in the page/DOM.
Steven