duanqiao0153 2015-06-23 05:20
浏览 106
已采纳

如何使用带脚本的codeIgniter动态添加字段?

i need add fields dynamically in my view form. i have a script function for that.. but i don't know how to include in that script into codeIgniter.

my controller -

class uploadfile extends CI_Controller {
 function __construct()
    {
     parent::__construct();
     $this->load->helper('url');}
 //index function
    function index()
    {
       //load file upload form
        $this->load->view('upload_file_view');}}

my view -

<script type="text/javascript" src="<?php echo base_url("assets/js/myscript.js");?>" ></script>
</head>
<body>
<label for="doc" class=" control-label">SUPPORT DOCUMENT</label>
         <div class="multi-field-wrapper ">
           <div class="multi-fields">
             <div class="multi-field">
 <input id="doc" type="text" class="form-control" name="attachment[]">
</div> </div>
<button type="button" class="add-field">Add field</button> </div>

and my script path - C:\xampp\htdocs\samplecod\assets\js

  • 写回答

1条回答 默认 最新

  • dongxie548548 2015-07-02 09:46
    关注

    i found a easy way to add multiple fields in view page

    `<div class="form-group">
                <div class="row colbox">
                <div class="col-sm-offset-2 col-lg-8 col-sm-8 text-left">Description</div>
    
                <div class="field_wrapper">
                    <input type="textarea" name="descrip[]" value="" size="35px" /><input type="text" name="voucher_no[]" value="" size="7px"/><input type="text" name="price[]" value=""size="7px"/>
                    <a href="javascript:void(0);" class="add_button" title="Add field"><img src="<?php echo base_url('images/add-icon.png'); ?>"/></a>
                </div></div></div>`
    

    for this form this is the script:

       <script type="text/javascript">
    $(document).ready(function(){
        var maxField = 20; //Input fields increment limitation
        var addButton = $('.add_button'); //Add button selector
        var wrapper = $('.field_wrapper'); //Input field wrapper
        var fieldHTML = '<div><input type="text" name="descrip[]" value="" size="35px"/><input type="text" name="voucher_no[]" value="" size="7px"/><input type="text" name="price[]" value="" size="7px"/><a href="javascript:void(0);" class="remove_button" title="Remove field"><img src="<?php echo base_url('images/remove-icon.png'); ?>"/></a></div>'; //New input field html 
        var x = 1; //Initial field counter is 1
        $(addButton).click(function(){ //Once add button is clicked
            if(x < maxField){ //Check maximum number of input fields
                x++; //Increment field counter
                $(wrapper).append(fieldHTML); // Add field html
            }
        });
        $(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
            e.preventDefault();
            $(this).parent('div').remove(); //Remove field html
            x--; //Decrement field counter
        });
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?