我有多個文本框,如何從中獲取值。從多個文本框中獲取值
$(function() {
$("#btnAdd").bind("click", function() {
var div = $("<div />");
div.html(GetDynamicTextBox(""));
$("#TextBoxContainer").append(div);
});
$("#btnGet").bind("click", function() {
var values = "";
$("input[name=a]").each(function() {
values += $(this).val() + "\n";
});
alert(values);
});
$("body").on("click", ".remove", function() {
$(this).closest("div").remove();
});
});
function GetDynamicTextBox(value) {
return '<input name = "a" type="text" value = "' + value + '" /> ' +
'<input type="button" value="Remove" class="remove" />'
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form method='POST' action='AddReqPo'>
<input id="btnAdd" type="button" value="Add" />
<br />
<br />
<div id="TextBoxContainer">
<!--Textboxes will be added here -->
</div>
<br />
<input id="btnGet" type="button" value="Get Values" />
<input type='submit'>
</form>
我要保存值的servlet。但是當按鈕提交發送值時,有錯誤java.lang.NullPointerException。這是servlet文件。當您提交表單(因爲你不會阻止默認與preventDefault()
提交操作)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String[] a=request.getParameterValues("a");
System.out.println(a[0]);
response.sendRedirect("index.jsp");
}
@RayonDabre這對PHP – Ramanlfc
@Ramanlfc,Yeah..you得到它right..http://stackoverflow.com/questions/11786486/sending-html-form- data-array-to-jsp-servlet可能會有所幫助! – Rayon
這是問題沒有驗證答案 –