【问题标题】:How to pass dynamically added textbox to spring MVC controller java如何将动态添加的文本框传递给spring MVC控制器java
【发布时间】:2019-03-04 10:34:52
【问题描述】:
    $("#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--;
}));

上面的代码是基于下拉选择选项添加一个文本框。它最多可以添加 3 个文本框。 如何将此文本框值传递给 Spring MVC 控制器。

【问题讨论】:

    标签: java spring spring-mvc model-view-controller


    【解决方案1】:

    您似乎正在使用 JQuery 来构建 UI。假设你有一个 Spring MVC 端点暴露在 POST http://localhost:8080/api/boxes 你可以使用 jQuery.ajax() 方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多