有没有更好的方法在javascript中编写以下条件?
if ( value == 1 || value == 16 || value == -500 || value == 42.42 || value == 'something' ) {
// blah blah blah
}
Run Code Online (Sandbox Code Playgroud)
我讨厌把所有这些逻辑OR串在一起.我想知道是否有某种速记.
谢谢!
在Firefox和Opera上的Javascript中使用indexOf调用获取错误.在IE中正常工作.
以下是错误消息:
行动
function anonymous(Grid, Row, Col, Event) {
return Grid.ActionShowPopupMenu();
}
Run Code Online (Sandbox Code Playgroud)
for事件OnRightClick失败,异常:row.id.indexOf不是函数
我正在测试一个字符串在Javascript中包含另一个字符串并使用字符串的indexOf函数.然而,调用是在JQuery函数中进行的.也许这就是问题的原因?是否有替代方法在Javascript中使用indexOf来测试字符串是否包含另一个字符串?这个问题有解决方法吗?
在javascript我正在做以下工作正常.
if (myVar == 25 || myVar == 26 || myVar == 27 || myVar == 28)
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能缩短它?类似以下内容.
if (myVar IN ('25','26','27','28')) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
要么
if(myVar.indexOf("25","26","27","28") > -1) ) {//do something}
Run Code Online (Sandbox Code Playgroud) 我有三个数组:
arr1=["14","16","1"] - 我在哪里选择arr2=["14"] - 我在哪里比较我的选择 arr1 arr3=[] - 我在推动价值的地方. 我怎样才能检查我的选择是否不存在arr2?
例如,我选择了14 arr1,因为它已经存在arr2,按钮将被禁用,不应该被推入arr3.
此函数在表单onSubmit期间执行,并且在Firefox和Chrome中正常工作,但在IE中则不行.我怀疑它是indexOf,但我似乎找不到让它工作的方法.
function checkSuburbMatch(e) {
var theSuburb = document.getElementById('suburb').value;
var thePostcode = document.getElementById('postcode').value;
var arrayNeedle = theSuburb + " (" + thePostcode + ")";
if(suburbs.indexOf(arrayNeedle) != -1) {
alert("Suburb and Postcode match!");
return false;
} else {
alert("Suburb and Postcode do not match!");
return false;
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下数组设置,我,e:
var myArray = new Array();
Run Code Online (Sandbox Code Playgroud)
使用这个数组,我会在用户添加更多菜单项时动态创建面包屑菜单.我还允许他们通过点击eatch breadcrumb菜单项旁边的十字架来删除特定的面包屑菜单项.
数组可能包含以下数据:
myArray[0] = 'MenuA';
myArray[1] = 'MenuB';
myArray[2] = 'MenuC';
myArray[3] = 'MenuD';
myArray[4] = 'MenuE';
Run Code Online (Sandbox Code Playgroud)
我的问题是:
a)在JavaScript中,如何从myArray中删除元素[1]然后重新计算索引或这是不可能的?
b)如果我不想要菜单选项MenuB,是否需要将其拼接以将其删除?
我的问题是,如果用户删除菜单项以及最后创建新闻,那么这些元素的索引将如何展开?
我只是希望能够删除项目,但不知道如何处理数组索引.
我在javascript函数中有一行,它根据另一个字符串数组的顺序对一个对象数组进行排序.这是在Firefox中工作但不在IE中,我不知道为什么.这是我的数据在IE7中进入排序调用的样子.(我正在使用三个项目的数组来说明这一点).
//cherry first then the rest in alphabetical order
originalData = ['cherry','apple','banana','clementine','nectarine','plum']
//data before sorting - note how clementine is second item - we wan to to to be after apple and banana
csub = [
{"value":"cherry","data":["cherry"],"result":"cherry"},
{"value":"clementine","data":["clementine"],"result":"clementine"},
{"value":"apple","data":["apple"],"result":"apple"},
{"value":"banana","data":["banana"],"result":"banana"},
{"value":"nectarine","data":["nectarine"],"result":"nectarine"},
{"value":"plum","data":["plum"],"result":"plum"}
]
//after sorting, csub has been rearranged but still isn't right: clementine is before banana. in FF it's in the right place.
csubSorted = [
{"value":"cherry","data":["cherry"],"result":"cherry"},
{"value":"apple","data":["apple"],"result":"apple"},
{"value":"clementine","data":["clementine"],"result":"clementine"},
{"value":"banana","data":["banana"],"result":"banana"},
{"value":"nectarine","data":["nectarine"],"result":"nectarine"},
{"value":"plum","data":["plum"],"result":"plum"}
]
Run Code Online (Sandbox Code Playgroud)
这是实际的排序代码:
csubSorted = csub.sort(function(a,b){ return (originalData.indexOf(a.value) …Run Code Online (Sandbox Code Playgroud) 我有一个要求,我根据JSON响应动态创建单选按钮.到目前为止,我所做的Chrome和Firefox的作品,但给Object doesn't support this property or method上if(subItem[1].indexOf(",") >= 0)线
我的代码
$("#sList").live("change", function(){
var currentService=this.value;
var c1Svc=[];
var c2Svc=[];
if(absP.length==2)
{
$.each(compareServiceData,function(i,item){
if(currentService==item[0])
{
var configCount=0;
$.each(item[1],function(j,subItem){
var temp=subItem[1];
/*The JSON response contains List of Lists, so here if it contains a list it will be separated by "," so split it and store in a array, else directly store in a array*/
if(subItem[1].indexOf(",") >= 0)
{
var tList=temp.split(",");
$.each(tList,function(k,val){
if(configCount==0)
{
c1Svc.push(val);
}
else
{
c2Svc.push(val); …Run Code Online (Sandbox Code Playgroud) html javascript jquery internet-explorer internet-explorer-8
javascript ×9
jquery ×4
indexof ×2
angularjs ×1
arrays ×1
html ×1
if-statement ×1
string ×1
testing ×1