无法从查询中删除jquery创建的元素

时间:2015-06-21 04:28:53

标签: javascript jquery

我用jquery创建了html但是我的删除逻辑不起作用。我想我错过了一些东西,但我找不到任何js错误。你能看出我哪里出错吗?

我的代码位于fiddle或下面的代码段中:



    function removedivs(idofr) {
      jQuery('#removediv' + idofr).remove();
    }
    jQuery(document).ready(function() {
      jQuery("#addnewr").click(function() {
        var appendid = parseInt(jQuery("#dupc > div#datav").length) + 1;
        jQuery("#dupc").append('<hr /><div id="datav removediv' + appendid + '"><input type="hidden" name="isd[]" value="' + appendid + '" />Event Name: <select name="event_name' + appendid + '"><option value="Ironman Race">Ironman Race</option><option value="Board Race">Board Race</option><option value="Ski Race">Ski Race</option><option value="Surf Race">Surf Race</option></select><p></p><p>Athlete Name:<br><select name="athlete_name' + appendid + '" id="athlete_name[]"><option selected="selected">Choose a user</option><?php echo $playerdd;?></select></p><p>Age Group: <select name="age_group' + appendid + '"><option value="Under 15">Under 15</option><option value="Under 17">Under 17</option><option value="Under 19">Under 19</option><option value="Open">Open</option></select></p><p>Sex: <input type="radio" name="athlete_sex' + appendid + '" value="Male">Male <input type="radio" name="athlete_sex' + appendid + '" value="Female">Female</p><p><input type="button" value="Remove it" onclick="removedivs(' + appendid + ')" /></p></div>');
      });
    });
&#13;
<form name="race_selection" method="POST" action="<?php echo the_permalink();?>?c=<?php echo $_GET['c'];?>">
  <div id="dupc">
    <div id="datav">
      <input type="hidden" name="isd[]" value="1" />Event Name:
      <select name="event_name1">
        <option value="Ironman Race">Ironman Race</option>
        <option value="Board Race">Board Race</option>
        <option value="Ski Race">Ski Race</option>
        <option value="Surf Race">Surf Race</option>
      </select>
      <p></p>
      <p>Athlete Name:
        <br>
        <select name="athlete_name1" id="athlete_name1">

          <option value="" selected="selected">Choose a user</option>
          <?php echo $playerdd;?>

        </select>
      </p>

      <p>Age Group:
        <select name="age_group1">
          <option value="Under 15">Under 15</option>
          <option value="Under 17">Under 17</option>
          <option value="Under 19">Under 19</option>
          <option value="Open">Open</option>
        </select>
      </p>

      <p>Sex:
        <input type="radio" name="athlete_sex1" value="Male">Male
        <input type="radio" name="athlete_sex1" value="Female">Female</p>
    </div>
  </div>
  <p>
    <input type="button" id="addnewr" value="Add New Race">
    <br>
    <p>
      <input type="submit" name="submit" value="Insert Race">
      <br>
      <p>
        <input type="reset" value="Reset!">
        <br>
      </p>
</form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我编辑了你的解决方案,这是解决方案。

jQuery('.removeInput').unbind('click').bind('click',function(){
       $('#datavremovediv' + $(this).attr('append')).remove();
    });

https://jsfiddle.net/rmzjoL4c/6/