<input type="submit" name="Post" value="Post" class="btm-bg" />
<script type="text/javascript" src="../Scripts/jquery-1.9.0.min.js"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/mvcfoolproof.unobtrusive.min.js")" type="text/javascript"></script>
$(function () {
$('.option').hide();
$('input[class="Tproperty"]').click(function () {
$('.option').hide();
if ($(this).val() == '2' || $(this).val() == '8') {
$('#Display').show();
$('#residential').hide();
$('#amen').hide();
}
if ( $(this).val() == '5') {
$('#Display').show();
$('#residential').hide();
$('#amen').hide();
$('#xtrafac').hide();
$('#parkings').hide();
$('#rooms').hide();
$('#bathrooms').hide();
}
else {
$('#Display').show();
$('#residential').show();
$('#amen').show();
$('#xtrafac').show();
$('#parkings').show();
$('#rooms').show();
$('#bathrooms').show();
}
});
});
$('form').on('submit', function () {
alert("hello");
var sr = $("input[name = saleorrent]:checked").val();
alert(sr);
var mr = $("input[name = Monthlyrent]").val();
alert(sr + mr);
if (sr == "Rent" && mr == null ) {
alert("Enter monthly rent");
e.preventDefault();
}
});
$(function () {
$('.showes').hide();
$('input[class="saleorrent"]').click(function () {
$('.showes').hide();
if ($(this).val() == 'Sale') {
$('#optionsale').show();
$('#sale').show();
$('#rent').hide();
}
else {
$('#optionRent').show();
$('#sale').hide();
$('#rent').show();
}
});
});
function CheckNumeric(e){
if (window.event) // IE
{
if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
event.returnValue = false;
return false;
}
}
else { // Fire Fox
if ((e.which < 48 || e.which > 57) & e.which != 8) {
e.preventDefault();
return false;
}
}
}
$(function () {
$("#City").change(function () {
if ($("#City").val() != 0) {
var ddlCityId = $(this).val();
var subItems = "";
$.getJSON("@Url.Action("ddlCities", "Listings")", { id: ddlCityId },
function (data) {
$.each(data, function (index, item) {
subItems += "<option value='" + item.Locality_Id + "'>" + item.Locality + "</option>"
});
$("#Locality").html(subItems)
});
}
else {
alert("false");
}
});
});
我正在使用MVC 4.0。如果字段留空,我想要一条警告消息。但事情是,只要我点击提交按钮,它就会到达服务器端,而不是在卸载页面之前执行jquery代码。
答案 0 :(得分:0)
beforeunload
会在表单提交时触发。您需要停止表单提交。例如像:
$('form').on('submit', function () {
alert("hello");
var sr = $("input[name = saleorrent]:checked").val();
alert(sr);
var mr = $("input[name = Monthlyrent]").val();
alert(sr+ mr);
if (sr == 'Rent' && mr == " ")
{
alert("Enter monthly rent");
e.preventDefault(); // <<<<<< Stop the form submission
}
});
备注:强>
始终使用复选框(&#39;选中&#39;)。
var sr = $("input[name=saleorrent]").prop('checked');