1

私は、実際にはモーダルウィンドウであるフォームを検証する次のajaxコードを作成しました。しかし、フォームを再度開いたときに閉じるまたは保存をクリックした後、値はまだそこにあります。以下は私のコードです:

<script>

$(function() {

    $("#submit_button").click(function( e ) {
        e.preventDefault();


        var name    = $("input[name=name]").val();
        var amount  = $("input[name=amount]").val();
        var model   = $("select[name=model]").val();
        var brand   = $("select[name=brand]").val(); 

        $.ajax({
                type: "POST",
                url: "<?=base_url()?>index.php/items/create",
                cache: false,
                dataType: "json",
                data: 'name='+name+'&amount='+amount+'&model='+model+'&brand='+brand,
                success: function(result){
                    if(result.error) {
            $(".alert").fadeIn('slow');
            $("#error_message").html(result.message);
          } else {            
            $(".alert").fadeIn('slow');
            $("#error_message").html(result.message);
            $('#new_item').modal('hide')
          }

                }
        }); 

    });
});

</script>

これはコントローラーコードです:

public function create()
    {
        //creating the required validation fields
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('brand', 'Brand', 'trim|required');
        $this->form_validation->set_rules('amount', 'Amount', 'trim|required|numeric');

        $result = array();

        if ($this->input->is_ajax_request()) 
        {

            if ($this->form_validation->run() == FALSE) {
                $result['error'] = true;
                $result['message'] = validation_errors();
            } else {

                $result['error'] = true;
                $result['message'] = 'The record has been saved';           

                $this->m_items->create();               
                redirect('items/index', 'refresh');
            }  

            $json = json_encode($result);
            die($json);
        }
        else
        {
            redirect('items/index', 'refresh');
        }
    }

2番目の問題は、[保存]をクリックした後、データベースにエントリが作成されてもメッセージが表示されず、フォームがその場所にとどまるということです。次に、ページを手動で更新して実行する必要があります。

誰かが親切にして私に解決策を教えてくれませんか.....私は誰もがこれまでにできる限り感謝します!

私は今3時間以上それを修正しようとしているので私を助けてください!

前もって感謝します!

4

1 に答える 1

0

それらを空にしないので?

$("input[name=name]").val("");
于 2012-10-30T08:41:04.503 に答える