2011-04-26 25 views
-1

我在我的應用程序中有3個複選框。每個複選框都對應一個文本框。當一個複選框被選中時,我想輸入對應的文本框的值。我使用表單驗證實用工具處理這個(事情。它的工作正常。但我的問題是,當我提交我的表單與複選框作爲選中,它成爲提交後取消選中。codeigniter中的複選框問題

+1

0出的9受理真的* *不是激發任何人的興趣的方式。 – 2011-04-26 04:10:17

回答

0

如果你想重新顯示值相同的形式,你也許已經在每個輸入字段的值屬性使用echo set_value('field_name')

<label for="field">Field</label> 
<input id="field" name="field" value="<?php echo set_value('field'); ?>" class="text" type="text"> 

隨着的複選框你使用set_checkbox功能這一點,方式:

<ul> 
    <li> 
     <input id="consult-option1" name="consult[]" value="option 1" <?php echo set_checkbox('consult[]', 'option 1') ?> class="checkbox" type="checkbox"> 
     <label for="consult-option1">Option 1</label> 
    </li> 
    <li> 
     <input id="consult-option2" name="consult[]" value="option 2" <?php echo set_checkbox('consult[]', 'option 2') ?> class="checkbox" type="checkbox"> 
     <label for="consult-option2">Option 2</label> 
    </li> 
    <li> 
     <input id="consult-option3" name="consult[]" value="option 3" <?php echo set_checkbox('consult[]', 'option 3') ?> class="checkbox" type="checkbox"> 
     <label for="consult-option3">Option 3</label> 
    </li> 
</ul> 

請注意echo set_checkbox('consult[]', 'option 1')裏面的輸入。如果應該檢查它,則迴應屬性checked="checked"。該函數位於Form助手內部,它接收兩個參數,複選框的名稱和值屬性中定義的複選框的值。

請讓我知道,如果它清楚,或者我應該編輯和寫得更清楚。再見

0

找了幾個小時沒有成功的答案後。 我想出了這個。 觀點:

<br/> 
    <input type="checkbox" id="active" <?=$is_active;?> name="active"> 
<br/>controller:<br/> 
    $this->form_validation->set_rules('active', lang('active'), 'required'); 
    <br/>if ($this->input->post('active') == 'on'){ // active input field is checked. 
    <br/>$data['is_active'] = 'checked'; 
    <br/>}else{ // active input field is unchecked. 
    <br/>$data['is_active'] = ''; 
    <br/>} 
<br/> 

在我而言,我不得不只檢查規則時選擇了另一個領域,所以我也用這個(但這只是可選)

if ($this->input->post('item_type') == '1') { 
$this->form_validation->set_rules('auction_time', lang('Auction time'), 'trim|required'); 
$this->form_validation->set_rules('active', lang('active'), 'required'); 
}