我有一个按钮,用于在我的页面上执行搜索操作。我正在使用jquery对5位数的邮政编码进行简单验证。如果邮政编码无效,我正在使用preventDefault来停止运行搜索按钮。但是,preventDefault方法似乎不起作用。即使邮政编码未正确验证,它仍会运行搜索。preventDefault无法阻止按钮在我的课程文件中调用方法
这里是我的jQuery代码:
var j$ = jQuery.noConflict();
j$(document).ready(function() {
var searchButton = j$('[id$=theFilterButton]');
var isValid = true;
searchButton.click(function(e) {
var zip = j$('[id$="ZipCodeFilter"]').val();
var reg = /^[0-9]+$/;
if((zip.length) < 5 || (zip.length) > 5) {
isValid = false;
console.log("zip code length is not 5");
}
if (!reg.test(zip)){
isValid = false;
console.log("zip is not digits");
}
if(!isValid) {
console.log("zip is not valid. Stop processing!!!!");
e.stopPropagation();
e.preventDefault();
return false;
}
});
});
不应该的preventDefault方法与调用我的代码在类文件停止按钮?
感谢您的任何帮助。 此致敬礼。
没有看到你的html,你试过'返回false;'而不是'preventDefault'? – 2014-11-04 17:26:56
不相关,但为什么不只是使用''#FilterButton''作为选择器? – 2014-11-04 17:30:07
我试图用相同的结果返回false。我没有使用'#theFilterButton'作为选择器,因为它是Salesforce内的VF代码,您必须使用上述方法选择VF标签。 – Dman100 2014-11-04 17:31:54