开发者

Debug, what's wrong with "$('#q li:last').remove();"

开发者 https://www.devze.com 2022-12-11 17:08 出处:网络
I want to remove the last <li></li>, below is the code: <div id=\"q\"> <ul><li>

I want to remove the last <li></li>, below is the code:

<div id="q">
    <ul><li>
    <table><tr><td>Question 1</td><td><input type="text" name="question1" size="60" /></td></tr>
   <tr><td>Answer:</td><td><input type="text" name="answer1" size="8" /></td></tr>
    </li></ul>
</div>

<input class="btn" id="addquestion"  type="button"  value="Add Question" />
<input class="btn" id="removequestion"  type="button"  value="Remove Question" />
<script>


 $('#addquestion').click(function() {
 var $question_number = $('#q li').size() + 1;

   $html='<li><table><tr><td>Question '+$question_number+'</td><td><input type="text" name="quest开发者_运维问答ion'+$question_number+'" size="60" /></td></tr>\
   <tr><td>Answer:</td><td><input type="text" name="answer'+$question_number+'" size="8" /></td></tr></li>';
  $('#q ul').append($html);
   });

  $('#addquestion').click(function() {
//$('#q li:last').remove();
}); 
</script>

When I comment out $('#q li:last').remove();, other part works fine. But if I remove comment, the code doesn't work at all. Any idea?


Um. Why is your add question removing a question?

Shouldn't removequestion remove the question?

$('#removequestion').click(function() {
    $('#q li:last').remove();
    return false;
}); 


I usually set up event handlers after the DOM has been loaded. ie, place them all within a $(document).ready() handler to avoid problems

looks like you have set up 2 click handlers. one adds the li and the other removes it ?? both will fire on a single click

0

精彩评论

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