2015-03-02 51 views
1

我有以下代碼。codeigniter:使用jQuery克隆方法驗證動態創建的文本域

<section class="section_class"> 
    <input type="text" name="branch_contact_person_name[]" value="<?php echo set_value('branch_contact_person_name'); ?>" placeholder="Branch Contact Person Name" /> 
    <input type="text" name="branch_contact_person_email[]" value="<?php echo set_value('shop_email'); ?>" placeholder="Branch Contact Person Email" /> 
    <input type="text" name="branch_contact_person_mobile[]" value="<?php echo set_value('shop_mobile'); ?>" placeholder="Branch Contact Person Mobile" /> 
    </section> 
<input type="hidden" class="add_fields" /> 

,我使用下面的jQuery

$(document).ready(function() { 
    $("#add_next_branch").click(function() { //add_next_branch is a button 
     $('.section_class').clone().appendTo('.add_fields'); 
    }); 
}); 

字段創建適當的,但是當我提交表單的所有文本字段爲空,然後它會顯示正確的消息,但文本字段被創文正在關閉。 這是控制器

function branch_details() { 
    $this->form_validation->set_rules('branch_contact_person_name[]', 'Site Name', 'required|xss_clean'); 
    /*$this->form_validation->set_rules('product_category', 'Product Category', 'trim|required|xss_clean'); //This field is hidden 
    $this->form_validation->set_rules('product_brand','Product Brand','required|greater_than[0]'); 
    //select location -- $this->form_validation->set_rules('shop_mobile', 'Shop Mobile', 'trim|required|min_length[6]|max_length[12]|xss_clean|numeric'); 
    //$this->form_validation->set_rules('brand_pic', 'Product Image', 'trim|required|xss_clean'); 
    $this->form_validation->set_rules('product_desc', 'Product Description', 'trim|required|xss_clean'); 
    $this->form_validation->set_rules('product_price_type', 'Product Price Type', 'required|xss_clean'); 
    $this->form_validation->set_rules('product_mrp', 'M.R.P ', 'required|xss_clean'); 
    */ 
    $data = array(); 
    $this->load->view('templates/homepage_header'); 
    if ($this->form_validation->run() == FALSE) 
    { 
     $data['action_name'] = 'branch_details'; 
    } 
    else 
    { 
     $data['action_name'] = 'shop_view'; 
    } 
     $this->load->view('shop_view',$data); 
} 
+4

你是什麼意思通過關閉.. – 2015-03-02 10:19:12

+0

我不明白你的問題,但你有一個錯字,你忘了關閉''''''''''''''class =「section_class」' – AdrienXL 2015-03-02 10:47:29

+0

熄滅 - 我的意思是說, add_field正在刪除d來自DOM – shridhar 2015-03-02 14:47:01

回答

0

您可以驗證克隆的元素,比如這個

function validate() 
    {  
     var contact_person_name= document.getElementById('formid').elements["branch_contact_person_name[]"]; 
     var contact_person_email=document.getElementById('formid').elements["branch_contact_person_email[]"]; 
     var contact_person_mobile=document.getElementById('formid').elements["branch_contact_person_mobile[]"]; 

     var length = contact_person_name.length; 

    if(isNaN(length)) 
    { 

     if(contact_person_name.value=='') 
     { 
      alert('Please enter name'); 
        return false; 
     } 
     if(contact_person_email.value=='') 
     { 
      alert('Please enter email'); 
        return false; 
     } 
     if(contact_person_mobile.value=='') 
     { 
      alert('Please enter Mobileno'); 
        return false; 
     } 
    } 

     for (var i = 0; i < contact_person_name.length; i++) 
     { 

      if (contact_person_name[i].value== '') { 
       alert('Please enter name.'); 
       return false; 
      } 
     if (contact_person_email[i].value== '') { 
       alert('Please enter email.'); 
       return false; 
      } 
     if (contact_person_mobile[i].value== '') { 
       alert('Please enter mobile.'); 
       return false; 
      } 
    } 
    document.yourform.submit(); 
    } 

確保選中聲明ID爲您HTML形式。這是可能有助於周到,謝謝