2013-05-02 362 views
0

我有一個窗體,我動態添加輸入字段(組)。JS - jQuery動態添加刪除輸入字段

這是一個相當複雜的形式,以及一部分可在這裏看到:FIDDLE

實際的錯誤,我得到的領事是:

Error: uncaught exception: query function not defined for Select2 s2id_autogen1 

當我已經在表單字段(例如前兩個)EDITREMOVE按鈕將工作得很好。

我的問題是,REMOVE按鈕(樣式輸入字段)不工作的動態ADDED場(實際上是「附加」的JS和PHP填充)的代碼

注:我知道代碼是一個爛攤子:-(。它繼承並即將清理。

它被複制,並從HTML輸出粘貼。

地址REMOVE編輯實際上風格像按鈕(過長和不相關貼)

的實際來源是PHP,它是跨越多個文件(因此是JS),因此有點太複雜,在這裏展示。

UPDATE:代碼按大家的要求:-)

public function show_field_repeater($field, $meta) { 
    global $post; 
    // Get Plugin Path 
    $plugin_path = $this->SelfPath; 
    $this->show_field_begin($field, $meta); 
    $class = ''; 
     if ($field['sortable']) 
     $class = " repeater-sortable"; 
    echo "<div class='at-repeat".$class."' id='{$field['id']}'>"; 

    $c = 0; 

    $meta = get_post_meta($post->ID,$field['id'],true); 


     if (count($meta) > 0 && is_array($meta)){ 
     foreach ($meta as $me){ 
      //for labling toggles 
      $mmm = isset($me[$field['fields'][0]['id']])? $me[$field['fields'][0]['id']]: ""; 
      echo '<div class="at-repater-block at-repater-block-'.$c.$field['id'].'"><h3>'.$mmm.' 
      <span class="at-re-remove"> 
      <input id="remove-'.$c.$field['id'].'" class="buttom button-primary" type="submitkb" value="Remove '.$field['name'].'" accesskey="x" name="removek"> 
      </span>'; 


      echo '<script> 
     jQuery(document).ready(function() { 
       jQuery("#remove-'.$c.$field['id'].'").on(\'click\', function() { 
        var answer = confirm("Are you sure you want to delete this field ??")  
        if(!answer){ 
         event.preventDefault();  
        } 


        jQuery(".at-repater-block-'.$c.$field['id'].'").remove(); 

       }); 
      }); 
     </script>'; 

      echo '<span class="at-re-toggle"> 
      <input id="edit-'.$field['id'].'" class="buttom button-primary" type="" value="Edit '.$field['name'].'" accesskey="p" name="editk"></h3> 
      </span> 

      <span style="display: none;"> 

      <table class="repeate-box wp-list-table widefat fixed posts" >'; 
      if ($field['inline']){ 
      echo '<tr class="post-1 type-post status-publish format-standard hentry category-uncategorized alternate iedit author-self" VALIGN="top">'; 
      } 
     foreach ($field['fields'] as $f){ 
      //reset var $id for repeater 
      $id = ''; 
      $id = $field['id'].'['.$c.']['.$f['id'].']'; 
      $m = isset($me[$f['id']]) ? $me[$f['id']]: ''; 
      $m = ($m !== '') ? $m : $f['std']; 
      if ('image' != $f['type'] && $f['type'] != 'repeater') 
      $m = is_array($m) ? array_map('esc_attr', $m) : esc_attr($m); 
      //set new id for field in array format 
      $f['id'] = $id; 
      if (!$field['inline']){ 
      echo '<tr>'; 
      } 
      call_user_func (array(&$this, 'show_field_' . $f['type']), $f, $m); 
      if (!$field['inline']){ 
      echo '</tr>'; 
      } 
     } 
     if ($field['inline']){ 
      echo '</tr>'; 
     } 
     echo '</table></span> 
     <span class="at-re-toggle"><img src="'; 
      if ($this->_Local_images){ 
      echo $plugin_path.'/images/edit.png'; 
      }else{ 
      echo 'http://i.imgur.com/ka0E2.png'; 
      } 
      echo '" alt="Edit" title="Edit"/></span> 
     <img src="'; 
     if ($this->_Local_images){ 
      echo $plugin_path.'/images/remove.png'; 
     }else{ 
      echo 'http://i.imgur.com/g8Duj.png'; 
     } 
     echo '" alt="'.__('Remove','mmb').'" title="'.__('Remove','mmb').'"></div>'; 
     $c = $c + 1; 

     } 
     } 

    echo '<img src="'; 
    if ($this->_Local_images){ 
     echo $plugin_path.'/images/add.png'; 
    }else{ 
     echo 'http://i.imgur.com/w5Tuc.png'; 
    } 
    echo '" alt="'.__('Add','mmb').'" title="'.__('Add','mmb').'" ><br/><input id="add-'.$field['id'].'" class="buttom button-primary" type="submitk" value="Add '.$field['name'].'" accesskey="q" name="addk"></div>'; 

    //create all fields once more for js function and catch with object buffer 
    ob_start(); 


    echo '<div class="at-repater-block">'; 


    echo '<table class="wp-list-table repeater-table">'; 


    if ($field['inline']){ 
     echo '<tr class="post-1 type-post status-publish format-standard hentry category-uncategorized alternate iedit author-self" VALIGN="top">'; 
    } 

    foreach ($field['fields'] as $f){ 
     //reset var $id for repeater 
     $id = ''; 
     $id = $field['id'].'[CurrentCounter]['.$f['id'].']'; 
     $f['id'] = $id; 

     if (!$field['inline']){ 
     echo '<tr>'; 
     } 

     if ($f['type'] != 'wysiwyg') 
     call_user_func (array(&$this, 'show_field_' . $f['type']), $f, ''); 
     else 
     call_user_func (array(&$this, 'show_field_' . $f['type']), $f, '',true); 
     if (!$field['inline']){ 
     echo '</tr>'; 
     } 
    } 
    $js_code2 ='<span class=\"at-re-remove\"><input id="remove-'.$c.$field['id'].'" class="buttom button-primary remove-'.$c.$field['id'].'" type="submi7" value="Removevv " accesskey="7" name="remove7"></span>'; 
    if ($field['inline']){ 
     echo '</tr>'; 
    } 



    $js_code2 = str_replace("\n","",$js_code2); 
    $js_code2 = str_replace("\r","",$js_code2); 
    $js_code2 = str_replace("'","\"",$js_code2); 
    echo $js_code2; 
    echo '</table><img src="'; 
    if ($this->_Local_images){ 
     echo $plugin_path.'/images/remove.png'; 
    }else{ 
     echo 'http://i.imgur.com/g8Duj.png'; 
    } 
    echo '" alt="'.__('Remove','mmb').'" title="'.__('Remove','mmb').'" ></div>'; 
    $counter = 'countadd_'.$field['id']; 
    $js_code = ob_get_clean(); 
    $js_code = str_replace("\n","",$js_code); 
    $js_code = str_replace("\r","",$js_code); 
    $js_code = str_replace("'","\"",$js_code); 
    $js_code = str_replace("CurrentCounter","' + ".$counter." + '",$js_code); 

    echo '<script> 
     jQuery(document).ready(function() { 
      var '.$counter.' = '.$c.'; 
      jQuery("#add-'.$field['id'].'").on(\'click\', function() { 
      '.$counter.' = '.$counter.' + 1; 
      jQuery(this).before(\''.$js_code.'\'); 
      // jQuery("#'.$field['id'].'").append(\''.$js_code2.'\'); 
      // alert(\''.$js_code2.'\'); 
      update_repeater_fields(); 
      }); 

      }); 
     </script>'; 
     echo '<script> 
     jQuery(document).ready(function() { 

       jQuery(".remove-'.$c.$field['id'].'").on(\'click\', function() { 
        var answer = confirm("Are you sure you want to delete this field ??")  
        if(!answer){ 
         event.preventDefault();  
        } 
        jQuery(".remove-'.$c.$field['id'].'").remove(); 
       }); 
      }); 
     </script>'; 
    echo '<br/><style> 
.at-inline{line-height: 1 !important;} 
.at-inline .at-field{border: 0px !important;} 
.at-inline .at-label{margin: 0 0 1px !important;} 
.at-inline .at-text{width: 70px;} 
.at-inline .at-textarea{width: 100px; height: 75px;} 
.at-repater-block{background-color: #FFFFFF;border: 1px solid;margin: 2px;} 
</style>'; 
    $this->show_field_end($field, $meta); 
    } 
+1

'.live()'被廢棄了,因爲jQuery的1.7,您應該切換到'。對()'。 – Barmar 2013-05-02 04:02:03

+0

感謝您的提示:-)會做。雖然我懷疑這不是問題在這裏:-) – 2013-05-02 04:04:34

+0

我沒有看到刪除按鈕被添加,你在說他們的「Removevv」的輸入字段? – Barmar 2013-05-02 04:05:20

回答

1

OK所以你已經被告知,現場已被棄用。

下面是該解決方案的小提琴:http://jsfiddle.net/y8JFb/2/

基本上給每個新的div你動態地創建基於您的櫃檯一個唯一的ID,然後給數據屬性包含該ID您刪除計數器。

然後你有你的點擊處理程序:

$(document).on("click", ".at-re-remove", function(e) { 
    $("#"+$(e.target).data("remove")).remove(); 
    $(e.target).remove(); 
}); 
+0

謝謝!此解決方案有效。 (只改變爲'jQuery(e.target).parent()。parent()。remove();'因爲DOM是如何在原始代碼中的。我希望我有一些JS技能。 (非常感謝大家)! – 2013-05-02 04:47:07