简体   繁体   中英

how to generate codeigniter's form_input using javascript

This is my form_input :

  <?php echo form_input('noRegistrasi', isset($noRegistrasi) ? $noRegistrasi : '', 'type="text" class="form-control input-sm" id="noRegistrasi" placeholder="No Registrasi" id="noRegistrasi"'); ?> 

I want it to be dynamic, so user can add more form_input and therefore, I need a Javascript function.

As comparison, this is my Javascript code to generate normal HTML input tag .

 function generateSUBKLP(index) { var idx = document.createElement("input"); idx.type = "text"; idx.name = "SUBKLP" + index + ""; idx.id = "SUBKLP[" + index + "]"; idx.size = "10"; return idx; } 

My question is: how do I generate codeigniter form_input() using Javascript?

If my question is not clear, please ask.^^

Here is the demo. Pls load jquery first

Html part:

<form method="post" action="collect_vals.php">
        <div id="input_fields">
            <div><input type="text" name="name[]"> <input type="text" name="project[]"> <span class="fa fa-plus-circle" id="add_field"></span></div>
        </div>
        <input type="submit" value="submit">
</form>

Javascript part:

$(document).ready(function() {
    $("#add_field").click(function(e){
        e.preventDefault();  
            $("#input_fields").append('<div><input type="text" name="name[]"/> <input type="text" name="project[]"> <span id="remove" class="fa fa-minus-circle"></span</div>'); 
    });

    $("#input_fields").on("click","#remove", function(e){ 
        e.preventDefault(); $(this).parent('div').remove();
    })
});

Also use codeigniter form_input like this

$(document).ready(function() {
    var input = "echo form_input('username', 'johndoe');";
    $('#input_fields').append("<div>"+input+"</div>");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM