简体   繁体   中英

How to pass dynamically added textbox to spring MVC controller java

    $("#addButton").click(function () {
    if(counter > 3){
            alert("Only 3 textboxes allowed");
            return false;
    }
    var selectfield = $('#selectcolumnlist option:selected').val();
    var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv');   
    newTextBoxDiv.after().html('<input type="text" name="textbox_' + selectfield + '" class="form-control" id="textbox_'+selectfield+'" placeholder="' + selectfield + '" value="" style="width: 400px;"/><input type="button" value="Remove Field" class="remove_this" id="removeid" accessKey="'+selectfield+'"/>');
    newTextBoxDiv.appendTo("#TextBoxesGroup");
    $('#selectcolumnlist option:selected').remove();
    counter++;
});

$("#TextBoxesGroup").on('click', '#removeid', (function() {
    var a = $(this).attr('accessKey');
    alert(a);
    $(this).parent('div').remove();
    $('#selectcolumnlist').append(new Option(a,a));
    counter--;
}));

Above code is adding a textbox based on the dropdown select option. It can add a maximum of 3 textboxes. How do I pass this textbox value to spring MVC controller.

It appears that you are using JQuery to build the UI. Assuming that you have a Spring MVC endpoint exposed at POST http://localhost:8080/api/boxes you can use jQuery.ajax() method:

$.ajax({
  method: "POST",
  url: "http://localhost:8080/api/boxes",
  data: { textbox: "value" }
})
.done(function(msg) {
  alert("Saved: " + msg);
});

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