防止两个默认功能发生,并同时触发第三个事件
Prevent 2 default functions from happening and trigger a third event at same time
问题听起来可能很简单。。。。。我试图实现的是,当表单验证失败时,我想要show a custom alert box
和prevent form from submitting
我被卡住的地方
我正在将Zebra_Dialog用于自定义警报框。。。现在,我知道e.preventDefault()
停止了默认功能的触发。。。。。但这只会停止一个事件
我该如何同时进行以下操作:
- 阻止出现默认警报框
- 阻止表单提交
- 显示自定义警报框
插件教程还提到了只防止默认警报框发生
HTML
<form id="searchform" class="input-append" method="post" action="search.php">
<select class="city shadow" name="city" id="city">
<option selected>Select your city</option>
<option value="AAAAA">AAAAA</option>
</select>
<select class="location shadow" name="location">
<option selected>Select your Location</option>
<option value="Loc">Loc</option>
<option value="Loc2">Loc2</option>
<option value="Loc3">Loc3</option>
</select>
<select class="gender shadow" name="gender">
<option selected>Select your gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<button type="submit" class="btn" name="search_room_for_me" id="search_room_for_me"> Search </button>
</form>
Jquery
<script>
$("#searchform").submit(function(e){
var city = $(".city").val();
var location = $(".location").val();
var gender = $(".gender").val();
if( city != "Select your city" )
{
if( location != "Select your Location")
{
if( gender != "Select your gender")
{
//submit form
}
else
{
e.preventDefault(); // here, default alert should stop
$.Zebra_Dialog('The link was clicked!');
alert("Invalid Gender Selected");
e.preventDefault(e); // prevent form submit
}
}
else
{
alert("Invalid Location Selected");
e.preventDefault(e);
}
}
else
{
alert("Invalid City Selected");
e.preventDefault(e);
}
});
</script>
有没有人尝试过类似的方法,或者有没有其他方法可以实现这一点!!??
对于任何未来的访问者:
我的订单错误
e.preventDefault(); // here, default alert should stop
$.Zebra_Dialog('The link was clicked!');
alert("Invalid Gender Selected");
e.preventDefault(e); // prevent form submit
相反,这起到了的作用
e.preventDefault(e);
$.Zebra_Dialog('<strong>Gender :</strong>,Select your gender ,
Please select a proper gender to proceed ',
{'type':'error','title':'Select Gender'});
原因:e.preventDefault(e);
中存在冲突,只有一个preventDefault(e)
就足以停止所有默认功能(力之前就知道!!)
相关文章:
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 如何使用PHP和JS级联三个下拉列表
- 灯光问题(使用三个.js)
- 在三个.js中创建球体(例如地球)的磁力线
- 什么'这是从第三个函数上的async 1st函数获得结果的更好方法
- javascript第三个XMLHttpRequest被拒绝.但前两个是允许的
- 将循环中的两个文本框相乘,并在第三个文本框上显示结果
- 根据前两个下拉ID显示第三个下拉列表
- 防止两个默认功能发生,并同时触发第三个事件
- 文本框事件中的第三个字符
- 第三个是bean.fire事件中的第三个参数
- j查询具有三个事件的方法
- Jquery - 键入事件检查三个字段中的输入值
- 只有三个事件侦听器
- 三个.js触摸事件延迟300毫秒
- 每次单击按钮时都会发生两个事件..但第三个事件仅在文档加载时更改
- 触发“三个”的点击事件.元素
- 三个下拉事件改变不工作
- 如何在Jquery或jJavascript中将三个事件绑定到同一个函数
- MVC下拉列表级联-如何在第一个ddl选择更改时在第三个ddl上触发事件