我正在尝试获取多个URL输入的值,如果URL的值无效,我只想为输入元素设置动画并停止所有内容。
有什么办法吗?
$('button').click(function(e){
var linkarr = [];
var $input = $('.default');
var isValidUrl = /[0-9a-z_-]+\.[0-9a-z_-][0-9a-z]/; // URLvalid check
$input.each(function() {
var inputVal = $(this).val();
if(!isValidUrl.test(inputVal)) {
$(this).parent().animateCss('shake');
// if input is not valid, I want to stop the code here.
}
if(inputVal) linkarr.push(inputVal);
});
e.preventDefault();
$.ajax({
url: '/api/compress',
type: 'POST',
dataType: 'JSON',
data: {url: linkarr},
success: function(data){ something
});
});
答案 0 :(得分:0)
一种方法是使用flag来检查和执行Ajax方法
RewriteRule mypage/page/([^/]*) /?p=main_page&page=$1 [L]
RewriteRule mypage/category/([^/]*) /?p=main_page&cid=$1 [L]
RewriteRule mypage/page/([^/]*)/category/([^/]*) /?p=main_page&page=$1&cid=$2 [L]
RewriteRule mypage /?p=main_page [L]

答案 1 :(得分:0)
您需要让每个循环外部都知道内容的条件。
$('button').click(function(e){
var linkarr = [];
var $input = $('.default');
var isValidUrl = /[0-9a-z_-]+\.[0-9a-z_-][0-9a-z]/; // URLvalid check
var blnIsValid = true;
$input.each(function() {
var inputVal = $(this).val();
if(!isValidUrl.test(inputVal)) {
$(this).parent().animateCss('shake');
// if input is not valid, I want to stop the code here.
// Input isn't valid so stop the code
blnIsValid = false;
return false; // Alternatively don't stop so that any other invalid inputs are marked
}
if(inputVal) linkarr.push(inputVal);
});
e.preventDefault();
// Check to make sure input is valid before making ajax call
if (blnIsValid) {
$.ajax({
url: '/api/compress',
type: 'POST',
dataType: 'JSON',
data: {url: linkarr},
success: function(data){ something
});
}
});