忽略最后一个 If 语句
Last If statement ignored
执行时忽略了我最后的if
语句。
尝试调试时,我在 if 语句中添加了console.log
,它按预期运行。
$(document).ready(function() {
// set HTML
var myVar = 'Null';
if ( document.location.href.indexOf('?1501aa') > -1 ) {
myVar = '01';
}
if ( document.location.href.indexOf('?1501ab') > -1 ) {
myVar = '02';
}
if ( document.location.href.indexOf('?1501ac') > -1 ) {
myVar = '03';
}
if ( document.location.href.indexOf('?1501ad') > -1 ) {
myVar = '04';
}
if ( document.location.href.indexOf('?1501ae') > -1 ) {
myVar = '05';
}
if ( document.location.href.indexOf('?1501af') > -1 ) {
myVar = '06';
}
if ( document.location.href.indexOf('?1501ag') > -1 ) {
myVar = '07';
}
if ( document.location.href.indexOf('?1501ah') > -1 ) {
myVar = '08';
//console.log('myVar set to' + myVar);
}
if ( document.location.href.indexOf('?1501aj') > -1 ) {
myVar = '09';
//console.log('myVar set to' + myVar);
}
else {
setText();
}
function setText() {
$('<label for="name">' + myVar + '</label>').insertBefore($('#name'));
}
});
jsFiddle 示例并不完美,因为它们处理 URL 的方式。
如何让最后一个 if 语句返回 true 并执行?
将setText()
执行从 if 结构的条件(else)部分中删除;除非不是值为 09,否则它不会运行,这使得它看起来像 09 不起作用......
if ( document.location.href.indexOf('?1501aj') > -1 ) {
myVar = '09';
//console.log('myVar set to' + myVar);
}
setText();
最初的问题已经由 rfornal 解释过了,但我想我应该提到这段代码确实需要干掉,所以没有那么多重复的代码。 有几种方法可以做到这一点。
根据找到的字符计算myVar
结果:
$(document).ready(function() {
var myVar = 'Null';
var match = window.location.search.match(/1501a(?)/);
if (match) {
var chr = match[1].charAt(0);
if (chr >= 'a' && chr <= 'h') {
var code = match[1].charCodeAt(0) - 'a'.charCodeAt(0) + 1;
myVar = '0' + code;
} else if (char === 'j') {
myVar = '09';
}
}
$('<label for="name">' + myVar + '</label>').insertBefore($('#name'));
});
而且,这是使用查找表的不同方法:
$(document).ready(function() {
var myVar = 'Null';
var matches = {'1501aa': '01', '1501ab': '02', '1501ac': '03', '1501ad': '04', '1501ae': '05',
'1501af': '06', '1501ag': '07', '1501ah': '08', '1501aj': '09'};
var match = window.location.search.match(/1501a?/);
if (match) {
var result = matches[match[0]];
if (result) {
myVar = result;
}
}
$('<label for="name">' + myVar + '</label>').insertBefore($('#name'));
});
此外,这些还:
- 使用
window.location.search
- 摆脱
setText()
函数,因为它不需要,单行代码可以直接执行 - 使用正则表达式查找匹配项
Wait what are we doing? I dunno here is this.
var myVar = "";
fucker("02","ab");
fucker("03","ac");
//and so on
$('<label for="name">' + myVar + '</label>').insertBefore($('#name'));
function fucker(num,word) {
if(document.location.href.indexOf("?1501"+word) > -1) {
myVar = num;
}
return;
}
这是一个使用 json 和循环的完全不同的迭代。
//Function for setting the label
function setText(myVar) {
$('<label for="name">' + myVar + '</label>').insertBefore($('#name'));
}
//used as the document.location.href
var url = "http://test.test.com?1501ad";
var myVar; //create the var but don't initialize it *note that creating myVar = 'Null'* does not create a null object. Though you don't need this. just overload the setText()
//create an array of values as a json object
var urls = [{"url": "?1501ab", "myVar": "01"},
{"url": "?1501ac", "myVar": "02"},
{"url": "?1501ad", "myVar": "03"},
{"url": "?1501ae", "myVar": "04"},
{"url": "?1501af", "myVar": "05"},
{"url": "?1501ag", "myVar": "06"},
{"url": "?1501ah", "myVar": "07"},
];
//loop through and only set text when it finds the proper url ending test by adjusting the url variable.
$.each(urls, function(key, value){
if(url.indexOf(value.url) > -1){
setText(value.myVar);
}
});
http://jsfiddle.net/538t9bfk/
你的if链很奇怪。我会使用 else if,然后最终的 else 将适用于所有 if 语句。现在,您的 else 仅适用于最后一个 if 块。
if ( document.location.href.indexOf('?1501aa') > -1 ) {
myVar = '01';
}
else if ( document.location.href.indexOf('?1501ab') > -1 ) {
myVar = '02';
}
else if ( document.location.href.indexOf('?1501ac') > -1 ) {
myVar = '03';
}
else if ( document.location.href.indexOf('?1501ad') > -1 ) {
myVar = '04';
}
else if ( document.location.href.indexOf('?1501ae') > -1 ) {
myVar = '05';
}
else if ( document.location.href.indexOf('?1501af') > -1 ) {
myVar = '06';
}
else if ( document.location.href.indexOf('?1501ag') > -1 ) {
myVar = '07';
}
else if ( document.location.href.indexOf('?1501ah') > -1 ) {
myVar = '08';
//console.log('myVar set to' + myVar);
}
else if ( document.location.href.indexOf('?1501aj') > -1 ) {
myVar = '09';
//console.log('myVar set to' + myVar);
} else {
//code what else you want to happen
}
setText();
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何在ReactJS JSX中执行嵌套的if-else语句
- 如何在 API 调用后和 if 语句中启用提交按钮
- 带有多个elseif的Javascript if语句
- Sharepoint JScript if语句未执行
- Javascript:If-then语句在函数中不起作用
- for循环中的javascript if语句找不到==
- jquery if语句返回return wong语句
- 在if语句下的html中使用javascript变量
- 将错误与if语句混淆
- 如何检查if/else语句中的随机条件
- 循环通过数组的If语句不起作用
- jQuery模板中的If语句
- 如何从数据库中回显If语句
- 如何减少if语句的数量
- Node.js-承诺和锥形语句(if、switch等)-如何构建
- 如何在嵌套ng重复指令中使用控制语句(if语句)
- 如何创建语句if包含"&"
- If语句- If主体有一个类remove在滚动原生javascript
- Javascript中的if语句- if - else问题