我有一个带有一个文本字段的表单。该表单是使用ajax提交的。提交时,我也想将文本字段中的数据保存在 ajax 变量之一中。下面是我的代码。
<script>
$(function() {
//twitter bootstrap script
$("button#submit").click(function(){
$.ajax({
type: "POST",
url: "PastSurgicalCustomItem",
data: $('form.form-horizontal').serialize(),
success: function(msg){
var $data = $('form.form-horizontal').serialize();
var $firstDiv = $('<div></div>');
$firstDiv.attr("class","col-md-3");
var $secondDiv = $('<div></div>');
$secondDiv.attr("class","checkbox history_checkbox_div");
$secondDiv.appendTo($firstDiv);
var $label = $('<label></label>');
$label.appendTo($secondDiv);
var $checkBox = $('<input></input>');
$checkBox.attr("type","checkbox");
$checkBox.attr("class","history_checkbox");
$checkBox.attr("id",msg);
$checkBox.appendTo($label);
var $span = $('<span></span>');
$span.text($data);
$span.appendTo($label);
$firstDiv.appendTo($('#customDiv'));
},
error: function(){
alert("failure");
}
});
});
});
</script>
但是效果并不好。如果我插入数据“test7”,那么 $data
变量将包含以下内容
textinput=&textarea=&customName=test7
如何正确地将文本框中的数据获取到ajax变量?有关更多信息,我的应用程序是使用 Bootstrap 设计的。
最佳答案
然后你可以在变量中存储这样的内容
var variable_name= $(this).find('textarea[name="your_textarea_name"]').text();
关于javascript - 如何将表单数据存储在ajax变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28694132/