jquery内部的Javascript抛出语句准备好了
javascript throw statement inside jquery ready?
所以我想知道如果以下是合法的,也许它不工作由于语法错误。对单个搜索字段进行四个规则的简单验证。感谢您对优雅和启发性解决方案的任何帮助!
$(function() {
$('input.text').focus(function() {
$(this).removeClass('noSubmitX');
$('.enterX').removeClass('now').text( orig);
}); //reset error status
var orig = "Enter the title you want.";
var msg1 = "Title must be between 3 and 63 characters.";
var msg2 = "Title cannot begin or end with a hypen";
var msg3 = "Title cannot contain a hyphen at character positions 3 and 4";
$('form.xSearch').submit(function() {
var theSearch = $('input.text').val();
var xLong = $('input.text').val().length;
var firstx = (theSearch[0]);
var thirdx = (theSearch[2]);
var fourthx = (theSearch[3]);
var lastx = (theSearch[xLong - 1]);
try {
if (xLong < 2 || xLong > 62) {
throw "msg1";
}
else if (firstx == "-") || (lastx == "-") {
throw "msg2";
}
else if (thirdx == "-") && (fourthx == "-")
{
throw "msg3";
}
}
catch (er) {
if (er == 'msg1') {
$('input.text').addClass('noSubmitX');
$('.enterX').addClass('now').text('Title must be between 3 and 63 characters.');
}
if (er == 'msg2') {
$('input.text').addClass('noSubmitX');
$('.enterX').addClass('now').text('Title cannot begin or end with a hypen');
}
if (er == 'msg3') {
$('input.text').addClass('noSubmitX');
$('.enterX').addClass('now').text('Title cannot contain a hyphen at character positions 3 and 4');
}
}
});
});
我想你的if
声明遇到麻烦了。它们需要用括号括起来,或者像这样括起来:
if (xLong < 2 || xLong > 62) {
throw "msg1";
}
else if (firstx == "-" || lastx == "-") {
throw "msg2";
}
else if (thirdx == "-" && fourthx == "-") {
throw "msg3";
}
相关文章:
- Electron:Express.js服务器在Mongo数据库'It’准备好了
- 我怎么知道Jqgrid已经准备好了
- Javascript+HTML中的日期选择器准备好了吗?(需要:DD、MM、YY的单独字段加上日历选择器)
- 杜兰达尔.js文件准备好了
- Jquery文档准备好了函数声明
- jquery在文档上发布准备好了
- JSON时调用函数.Parse() 已经准备好了
- 在页面底部加载脚本是否保证在执行任何脚本之前DOM都准备好了
- FileReader JS Api在它之前调用Fire'It’准备好了
- 为什么Internet Explorer有时会在数据'It’准备好了
- Knockback.js制作准备好了吗
- getElementById有时为null.已经试过了.加载完毕.准备好了
- javascript作用域和jquery's$(文档).准备好了
- Jquery(美元).val ();准备好了,不工作了
- jQuery data()在窗口.加载和文档.准备好了,没有定义
- Jquery滑块加载图像时,准备好了占位符
- Javascript函数不能在jquery $(document)中工作.准备好了
- 我如何选择元素后添加的类名文档准备好了
- jQuery.准备好了
- jquery内部的Javascript抛出语句准备好了