2009-09-04 104 views
0

我有一個使用生成的HTML jQuery和它就像未知元素點擊

<p class="fieldChoices title" style=""> 
    Choices: 
    <input id="Choice1" value="option1" maxlength="150"/> 
    <div class="seperator"/> 
    <p class="deleteChoice1 cutsom_button deleteField"></p> 
    <div class="seperator"/> 
    <input id="Choice2" value="option2" maxlength="150"/> 
    <div class="seperator"/> 
    <p class="deleteChoice2 cutsom_button deleteField"></p> 
    <div class="seperator"/> 
</p> 

我對deleteChoice1的點擊相應的選擇1必須從使用JQuery的.fieldChoices被刪除試圖..

此外,我可能不知道的JQuery我是否點擊deleteChoice1/deleteChoice2 ,, ,所以我不知道如何使用JQuery..please解決它建議我....

回答

2
$(".deleteField").click(function(){ 
    $(this).prevAll("input:first").remove(); 
    $(this).prevAll(".seperator").remove(); 
    $(this).remove(); 
}); 

雖然如果您將每個選項放入div,它會更容易。

1

嘗試以下操作來代替:

<p class="fieldChoices title" style=""> 
    Choices: 
    <fieldset> 
    <input id="Choice1" value="option1" maxlength="150"/> 
    <div class="seperator"/> 
    <span class="cutsom_button deleteField"></span> 
    </fieldset> 
    <fieldset> 
    <input id="Choice2" value="option2" maxlength="150"/> 
    <div class="seperator"/> 
    <span class="cutsom_button deleteField"></span> 
    </fieldset> 
</p> 

$("deleteField").bind("click", function(){ 
    $(this).parent().remove(); 
} 
+0

哦,良好的通話。否則,外部元素仍然存在。要編輯我的! – Anthony 2009-09-04 07:01:58

0

HTML:

<fieldset class="fieldChoices title"> 
    <legend>Choices</legend> 
    <ul> 
    <li> 
     <input id="Choice1" value="option1" maxlength="150"/> 
     <span class="deleteChoice cutsom_button deleteField"></span> 
    </li> 
    <li> 
     <input id="Choice2" value="option2" maxlength="150"/> 
     <span class="deleteChoice cutsom_button deleteField"></span> 
    </li> 
    </ul> 
</fieldset> 

的jQuery:

$(".fieldChoices li").each(function() { 
     var choice = $(this); 
     $(".deleteChoice", this).click(function() { 
      choice.remove(); 
     }); 
     });