我有一个循环中生成的带有多个文本区域的视图。我想使用jQuery在表单提交中传递值。我使用以下代码调用操作结果:
var url = "/Template/DATA";
var form = $('<form action="' + url + '" method="post">' +
'<input type="text" name="TemplateID" value="' + TemplateID + '" />' +
'<input type="text" name="SectionID" value="' + SectionID + '" />' +
'<input type="text" name="StartDate" value="' + StartDate + '" />' +
'<input type="text" name="EndDate" value="' + EndDate + '" />' +
// here i want to pass the values of the text areas to the action result
'</form>');
$('body').append(form);
form.submit();
使用html / jquery / ajax怎么办?
<form id="form" action="" method="post">
<input type="text" id="template" name="TemplateID" value="" />
<input type="text" id="section" name="SectionID" value="" />
<input type="text" id="start" name="StartDate" value="" />
<input type="text" id="end "name="EndDate" value="" />
<button type="submit">Submit</button>
</form>;
从文件加载中给from赋值并序列化数据以进行发送。
jQuery(document).ready(function ($) {
$('#template').val('Template');
$('#section').val('section');
$('#start').val('start');
$('#end').val('end');
$('#form').on('submit', function (e) {
e.preventDefault();
$.ajax({
url: 'js/ajax.php',
type: 'POST',
data: $(this).serialize(),
dataType: 'json',
beforeSend: function () {
},
success: function (data, textStatus, xhr) {
console.log(data);
console.log(data.status);
},
error: function (xhr, textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
console.log(data.status);
}
});
});
});
然后像访问其他任何发布数据一样访问php中的数据
if($_POST){
$template = $_POST['template'];
$section = $_POST['section'];
$start = $_POST['start'];
$end = $_POST['end'];
/** Do whatever with this data */
$result = $template . $section . $start . $end
echo json_encode(array('status' => 'ok', 'result' => $content));
} else {
echo json_encode(array('status' => 'error'));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句