У меня есть форма Html, в которой есть кнопка отправки, и внутри формы у меня есть несколько разделов, которые необходимо отправить отдельно. Поскольку Html не поддерживает вложенные формы, как я могу это сделать? А также каждый раздел должен быть проверен перед отправкой.
<% using (Html.BeginForm("actionName", "controllerName", FormMethod.Post))
{ %>
<form id="frmHomeContact" method="post" >
-- a partial view is rendered here
<div class="grid-2-12">
<input style="width: 100%;" type="button" title="save" value="save" />
</div>
</form>
<%}%>
Дайте мне знать, если вопрос непонятен.
$('#frmHomeContact').on('submit', function(){
var $this = $(this); // catch a reference to the form
$.ajax({
url: '/', //this should probably be the path to your controller
type: 'POST',
data: $this.serialize(), //name=George&msg=hello..etc <--note $this not $(this)
success: function(){
console.log('Successful submission.');
}
});
return false; //submitting the form will no longer cause the browser to refresh.
});
$('#frmHomeContact').submit(
если вы используете версию ниже 1.7
Ваш html-код выглядит так:
<% using (Html.BeginForm("actionName", "controllerName", FormMethod.Post),new { id = "frmHomeContact" })
{ %>
<form method="post" >
-- a partial view is rendered here
<div class="grid-2-12">
<input style="width: 100%;" type="button" title="save" value="save" />
</div>
</form>
<%}%>
<script type="text/javascript">
$("#frmHomeContact").submit(function() {
var $this = $(this); // catch a reference to the form
$.ajax({
url: '/', //this should probably be the path to your controller
type: 'POST',
data: $this.serialize(), //name=George&msg=hello..etc <--note $this not $(this)
success: function(){
alert('Successful submission.');
}
});
return false; //submitting the form will no longer cause the browser to refresh.
});
</script>
Вы можете получить значение отдельного текстового поля, используя var txtname= $('#txtid').val();
и проверьте, пусто ли имя txt или нет.
jQuery.post()
=)