If/else语句:搜索多个可能的值,只查找最后一个值(javascript)
我有一个带有各种输入的表单来搜索地址。加载页面时,只有省和邮政编码第一次出现,但可以选择打开切换以显示更多选项,如街道名称等。基本上,我试图实现的是,如果选择和文本输入不为空(“”和0),则在再次加载页面时,切换将保持打开状态 我的大部分逻辑都是错误的,但我的问题是if/else语句只查看它得到的最后一个值If/else语句:搜索多个可能的值,只查找最后一个值(javascript),javascript,jquery,if-statement,Javascript,Jquery,If Statement,我有一个带有各种输入的表单来搜索地址。加载页面时,只有省和邮政编码第一次出现,但可以选择打开切换以显示更多选项,如街道名称等。基本上,我试图实现的是,如果选择和文本输入不为空(“”和0),则在再次加载页面时,切换将保持打开状态 我的大部分逻辑都是错误的,但我的问题是if/else语句只查看它得到的最后一个值 var allInputsValue; var allInputs; //for testing purposes $('.address-options').each( func
var allInputsValue;
var allInputs; //for testing purposes
$('.address-options').each(
function(){
allInputs = $(this); //for testing purposes
allInputsValue = $(this).val();
//console.log('Type: ' + allInputs.attr('type') + ' Name: ' + allInputs.attr('name') + ' Value: ' + allInputs.val());
//console.log(' ID: ' + allInputs.attr('id') + '\n' + ' Value: ' + allInputs.val());
console.log('Value: ' + allInputsValue);
}
);
//if($('#civicNo').val() == '', $('#streetName').val() == ''){
if(allInputsValue === '' || allInputsValue === 0){
alert("Empty fields");
if (localStorage) {
localStorage.setItem('addressOptions', 'closed');
}
togglePane("closed");
}else{
alert("Fields not empty");
if (localStorage) {
localStorage.setItem('addressOptions', 'open');
}
togglePane("open");
}
我试图保持代码相当干净,而不必为每个输入ID执行else if
在控制台日志中,我返回了我要查找的所有正确值。我遗漏了什么?你需要把
如果还有放在里面。每个
var allInputsValue;
var allInputs; //for testing purposes
$('.address-options').each(
function(){
allInputs = $(this); //for testing purposes
allInputsValue = $(this).val();
//console.log('Type: ' + allInputs.attr('type') + ' Name: ' + allInputs.attr('name') + ' Value: ' + allInputs.val());
//console.log(' ID: ' + allInputs.attr('id') + '\n' + ' Value: ' + allInputs.val());
console.log('Value: ' + allInputsValue);
//if($('#civicNo').val() == '', $('#streetName').val() == ''){
if(allInputsValue === '' || allInputsValue === 0){
alert("Empty fields");
if (localStorage) {
localStorage.setItem('addressOptions', 'closed');
}
togglePane("closed");
}else{
alert("Fields not empty");
if (localStorage) {
localStorage.setItem('addressOptions', 'open');
}
togglePane("open");
}
}
);
您需要将if-else
放入中。每个
var allInputsValue;
var allInputs; //for testing purposes
$('.address-options').each(
function(){
allInputs = $(this); //for testing purposes
allInputsValue = $(this).val();
//console.log('Type: ' + allInputs.attr('type') + ' Name: ' + allInputs.attr('name') + ' Value: ' + allInputs.val());
//console.log(' ID: ' + allInputs.attr('id') + '\n' + ' Value: ' + allInputs.val());
console.log('Value: ' + allInputsValue);
//if($('#civicNo').val() == '', $('#streetName').val() == ''){
if(allInputsValue === '' || allInputsValue === 0){
alert("Empty fields");
if (localStorage) {
localStorage.setItem('addressOptions', 'closed');
}
togglePane("closed");
}else{
alert("Fields not empty");
if (localStorage) {
localStorage.setItem('addressOptions', 'open');
}
togglePane("open");
}
}
);
您需要将if-else
放入中。每个
var allInputsValue;
var allInputs; //for testing purposes
$('.address-options').each(
function(){
allInputs = $(this); //for testing purposes
allInputsValue = $(this).val();
//console.log('Type: ' + allInputs.attr('type') + ' Name: ' + allInputs.attr('name') + ' Value: ' + allInputs.val());
//console.log(' ID: ' + allInputs.attr('id') + '\n' + ' Value: ' + allInputs.val());
console.log('Value: ' + allInputsValue);
//if($('#civicNo').val() == '', $('#streetName').val() == ''){
if(allInputsValue === '' || allInputsValue === 0){
alert("Empty fields");
if (localStorage) {
localStorage.setItem('addressOptions', 'closed');
}
togglePane("closed");
}else{
alert("Fields not empty");
if (localStorage) {
localStorage.setItem('addressOptions', 'open');
}
togglePane("open");
}
}
);
您需要将if-else
放入中。每个
var allInputsValue;
var allInputs; //for testing purposes
$('.address-options').each(
function(){
allInputs = $(this); //for testing purposes
allInputsValue = $(this).val();
//console.log('Type: ' + allInputs.attr('type') + ' Name: ' + allInputs.attr('name') + ' Value: ' + allInputs.val());
//console.log(' ID: ' + allInputs.attr('id') + '\n' + ' Value: ' + allInputs.val());
console.log('Value: ' + allInputsValue);
//if($('#civicNo').val() == '', $('#streetName').val() == ''){
if(allInputsValue === '' || allInputsValue === 0){
alert("Empty fields");
if (localStorage) {
localStorage.setItem('addressOptions', 'closed');
}
togglePane("closed");
}else{
alert("Fields not empty");
if (localStorage) {
localStorage.setItem('addressOptions', 'open');
}
togglePane("open");
}
}
);
它可能只是我的眼睛,但看起来你的if/else语句在循环之外。它需要在循环中
var allInputsValue;
var allInputs; //for testing purposes
$('.address-options').each(
function(){
allInputs = $(this); //for testing purposes
allInputsValue = $(this).val();
//console.log('Type: ' + allInputs.attr('type') + ' Name: ' + allInputs.attr('name') + ' Value: ' + allInputs.val());
//console.log(' ID: ' + allInputs.attr('id') + '\n' + ' Value: ' + allInputs.val());
console.log('Value: ' + allInputsValue);
}
// ); This needs to be below
//if($('#civicNo').val() == '', $('#streetName').val() == ''){
if(allInputsValue === '' || allInputsValue === 0){
alert("Empty fields");
if (localStorage) {
localStorage.setItem('addressOptions', 'closed');
}
togglePane("closed");
}else{
alert("Fields not empty");
if (localStorage) {
localStorage.setItem('addressOptions', 'open');
}
togglePane("open");
}
**);**
它可能只是我的眼睛,但看起来你的if/else语句在循环之外。它需要在循环中
var allInputsValue;
var allInputs; //for testing purposes
$('.address-options').each(
function(){
allInputs = $(this); //for testing purposes
allInputsValue = $(this).val();
//console.log('Type: ' + allInputs.attr('type') + ' Name: ' + allInputs.attr('name') + ' Value: ' + allInputs.val());
//console.log(' ID: ' + allInputs.attr('id') + '\n' + ' Value: ' + allInputs.val());
console.log('Value: ' + allInputsValue);
}
// ); This needs to be below
//if($('#civicNo').val() == '', $('#streetName').val() == ''){
if(allInputsValue === '' || allInputsValue === 0){
alert("Empty fields");
if (localStorage) {
localStorage.setItem('addressOptions', 'closed');
}
togglePane("closed");
}else{
alert("Fields not empty");
if (localStorage) {
localStorage.setItem('addressOptions', 'open');
}
togglePane("open");
}
**);**
它可能只是我的眼睛,但看起来你的if/else语句在循环之外。它需要在循环中
var allInputsValue;
var allInputs; //for testing purposes
$('.address-options').each(
function(){
allInputs = $(this); //for testing purposes
allInputsValue = $(this).val();
//console.log('Type: ' + allInputs.attr('type') + ' Name: ' + allInputs.attr('name') + ' Value: ' + allInputs.val());
//console.log(' ID: ' + allInputs.attr('id') + '\n' + ' Value: ' + allInputs.val());
console.log('Value: ' + allInputsValue);
}
// ); This needs to be below
//if($('#civicNo').val() == '', $('#streetName').val() == ''){
if(allInputsValue === '' || allInputsValue === 0){
alert("Empty fields");
if (localStorage) {
localStorage.setItem('addressOptions', 'closed');
}
togglePane("closed");
}else{
alert("Fields not empty");
if (localStorage) {
localStorage.setItem('addressOptions', 'open');
}
togglePane("open");
}
**);**
它可能只是我的眼睛,但看起来你的if/else语句在循环之外。它需要在循环中
var allInputsValue;
var allInputs; //for testing purposes
$('.address-options').each(
function(){
allInputs = $(this); //for testing purposes
allInputsValue = $(this).val();
//console.log('Type: ' + allInputs.attr('type') + ' Name: ' + allInputs.attr('name') + ' Value: ' + allInputs.val());
//console.log(' ID: ' + allInputs.attr('id') + '\n' + ' Value: ' + allInputs.val());
console.log('Value: ' + allInputsValue);
}
// ); This needs to be below
//if($('#civicNo').val() == '', $('#streetName').val() == ''){
if(allInputsValue === '' || allInputsValue === 0){
alert("Empty fields");
if (localStorage) {
localStorage.setItem('addressOptions', 'closed');
}
togglePane("closed");
}else{
alert("Fields not empty");
if (localStorage) {
localStorage.setItem('addressOptions', 'open');
}
togglePane("open");
}
**);**
您正在覆盖每个.each()
中的allInputsValue。将if/else
放入中的.each()
,或者将allInputsValue设置为一个数组。您正在覆盖每个中的allInputsValue.each()
。将if/else
放入中的.each()
,或者将allInputsValue设置为一个数组。您正在覆盖每个中的allInputsValue.each()
。将if/else
放入中的.each()
,或者将allInputsValue设置为一个数组。您正在覆盖每个中的allInputsValue.each()
。要么将if/else
放在的.each()
中,要么将其allInputsValue设置为数组。你知道为什么值为0会导致字段不为空吗?我还用这个解决方案进行了5次切换:/知道为什么值为0会导致字段不为空吗?我还用这个解决方案进行了5次切换:/知道为什么值为0会导致字段不为空吗?我还用这个解决方案进行了5次切换:/知道为什么值为0会导致字段不为空吗?我还用这个解决方案进行了5次切换:/知道为什么值为0会导致字段不为空吗?我还使用了5个切换来结束这个解决方案:/@Jessica尝试了'0'
而不是if
语句中的0
?我觉得不尝试这个很傻。谢谢它解决了这个问题。但是现在我有七个开关,每次字段打开或为空时都会创建它们。。。我没有想到,这个逻辑可能不起作用。你知道为什么值为0会导致字段不为空吗?我还使用了5个切换来结束这个解决方案:/@Jessica尝试了'0'
而不是if
语句中的0
?我觉得不尝试这个很傻。谢谢它解决了这个问题。但是现在我有七个开关,每次字段打开或为空时都会创建它们。。。我没有想到,这个逻辑可能不起作用。你知道为什么值为0会导致字段不为空吗?我还使用了5个切换来结束这个解决方案:/@Jessica尝试了'0'
而不是if
语句中的0
?我觉得不尝试这个很傻。谢谢它解决了这个问题。但是现在我有七个开关,每次字段打开或为空时都会创建它们。。。我没有想到,这个逻辑可能不起作用。你知道为什么值为0会导致字段不为空吗?我还使用了5个切换来结束这个解决方案:/@Jessica尝试了'0'
而不是if
语句中的0
?我觉得不尝试这个很傻。谢谢它解决了这个问题。但是现在我有七个开关,每次字段打开或为空时都会创建它们。。。我没想过,这个逻辑可能行不通。