java - 如何将动态添加的文本框传递给spring MVC控制器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--;
}));
上面的代码是基于下拉选择选项添加一个文本框。它最多可以添加 3 个文本框。如何将此文本框值传递给 spring MVC 控制器。
解决方案
您似乎正在使用 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);
});
推荐阅读
- javascript - 为什么我的本地存储变量都返回 true?
- mysql - 创建每个项目的订单降序列表并在单独的列中显示排名位置
- python - Python 日志记录:使用配置文件的 ISO8601 时间戳和毫秒和时区
- python - TensorFlow - ValueError:形状 (3, 1) 和 (4, 3) 不兼容
- sql-server - ntext参数的存储过程cut值
- html - 如何垂直居中两个子元素?
- java - 如果这些线程从不修改同一个项目,是否可以从多个线程修改 ArrayList 中的项目?
- angular - Angular HighCharts 更新系列
- python - 使用 lambda 函数的 Python 列表排序
- python - Python:需要在箱线图中重叠实际数据