我有一个表单(一个文本字段和提交按钮)。用户输入将是一个3位数字,如果数字大于200
,则会显示警告。如果用户点击OK
,则必须提交表单,但如果他点击CANCEL
则没有任何反应,用户将能够再次提供一些价值。
事实是我的表格在任何情况下都是提交的(我很难注意到警告提示)。这是代码:
<form name="pontoi" action="update.php" method="post">
<div class="col-sm-4"><br>
<div class="input-group">
<input type="text" class="form-control" autofocus="autofocus" onkeyup="checkInput(this)" maxlength="3" id="points" name="points">
<span class="input-group-btn">
<button type="submit" id="prosthiki" onclick="checkInp(this)" class="btn btn-primary">ΠΡΟΣΘΗΚΗ</button>
</span>
</div>
</div>
</div>
</form>
function checkInp(e) {
var x = document.forms["pontoi"]["points"].value;
var y = 200;
if (x > y) {
sweetAlert({
title: "Number is over 200",
text: "Are you sure?",
type: "warning",
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true,
});
} else {
return false;
}
}
如果您对提交的表格有什么想法,这对我有很大的帮助。
还有这个脚本(我认为最好使用它),但它也不起作用..
$('.btn btn-primary').on('click',function checkInp(e){
e.preventDefault();
var form = $(this).parents('pontoi');
swal({
title: "Number is over 200",
text: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "OK",
closeOnConfirm: false
}, function checkInp(isConfirm){
if (isConfirm) form.submit();
});
}
});
答案 0 :(得分:2)
您还必须收听表单的onsubmit事件。
从按钮停止事件传播无效。
您可以做的是直接挂钩提交事件而不是点击按钮
public class CustomValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
OperationResult retVal = new OperationResult()
{
ReturnCode = 0,
ReturnMessage = "OK"
};
string s_userName = ConfigurationManager.AppSettings["username"].ToString();
string s_password = ConfigurationManager.AppSettings["password"].ToString();
if (userName == null || password == null)
{
throw new ArgumentNullException();--I dont want
return retVal; --It is good for me
}
if (userName == s_userName && password == s_password)
{
return;
}
else
{
//return;
throw new FaultException("-2 Unauthorized");--I dont want this
return retVal; --This giving problem.How can I solve?
}
}
}