Javascript Jquery - if 语句中的运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3780399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery - operators in if statement
提问by James
I'm trying to submit a form to a different location depending on which (of two) form fields have been filed in.
我正在尝试将表单提交到不同的位置,具体取决于已提交的(两个)表单字段。
It work when both fields have a value but when only one if field has a value it always submits to the suppliers/category/ URL. Here is my code.
当两个字段都有一个值但只有一个 if 字段有一个值时,它总是会提交给供应商/类别/ URL。这是我的代码。
$('#suppliersForm').submit(function() {
catVal = $('#category').val()
keywordVal = $('#keywords').val()
if( $('#category').val() && $('#keywords').val() )
{
var searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/searchresults/' + catVal + '/' + keywordVal
window.location.replace(searchString);
}
else if( $('#category').val() || $('#keywords').val() )
{
var searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/category/' + catVal
window.location.replace(searchString);
}
else if( $('#keywords').val() || $('#category').val() )
{
var searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/keywords/' + keywordVal
window.location.replace(searchString);
}
return false;
});
Any help would be appreciated
任何帮助,将不胜感激
采纳答案by trrrrrrm
$('#suppliersForm').submit(function() {
var catVal = $('#category').val();
var keywordVal = $('#keywords').val();
var searchString = "";
if( catVal != "" && keywordVal !="" )
{
searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/searchresults/' + catVal + '/' + keywordVal;
}
else if( catVal != "" )
{
searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/category/' + catVal;
}
else if( keywordVal != "" )
{
searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/keywords/' + keywordVal;
}
window.location.replace(searchString);
return false;
});
回答by Naveed
jQuery('#suppliersForm').click(function(event){
var searchString = '';
var catVal = $('#category').val();
var keywordVal = $('#keywords').val();
if( catVal.length > 0 && keywordVal.length > 0 ){
searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/searchresults/' + catVal + '/' + keywordVal;
} else if( catVal.length > 0 ) {
searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/category/' + catVal;
} else if( keywordVal.length > 0 ) {
searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/keywords/' + keywordVal
} else {
alert( 'Please enter at least on value' );
}
window.location.replace(searchString);
});
回答by user113716
I'd probably make it much more concise like this:
我可能会让它更简洁:
var catVal = $('#category').val()
, keywordVal = $('#keywords').val()
, searchString = 'http://<cfoutput>#cgi.server_name#</cfoutput>/suppliers/';
if( catVal && keywordVal )
searchString += ('searchresults/' + catVal + '/' + keywordVal);
else if( catVal )
searchString += ('category/' + catVal);
else if( keywordVal )
searchString += ('keyword/' + keywordVal);
else return;
window.location.replace(searchString);
return false;
回答by G?T?
Remove the second checks on both else if:
如果出现以下情况,请删除对其他两项的第二项检查:
else if( $('#category').val() || $('#keywords').val() ) --> else if( $('#category').val() )
else if( $('#keywords').val() || $('#category').val() ) --> else if( $('#keywords').val() )
回答by Tumas
Basically there is no difference between
基本上没有区别
else if( $('#category').val() || $('#keywords').val() )
and
和
else if( $('#keywords').val() || $('#category').val() )
So if at least one field contains some value the first else if is executed.
因此,如果至少一个字段包含某个值,则执行第一个 else if。