JavaScript是否有方便的方法来测试变量是否匹配多个值中的一个?
这是我的代码,
function Start()
{
if(number==(0||3||6||8||9||11||13||14||15||18||19||22||23||25||27||28||31||34||43||46||47||49||54||58||59||62||63||68||71||74||75))
{
FirstFunction();
}
if(number==(1||4||5||7||12||16||17||20||21||26||29||32||33||42||45||48||50||51||53||55||56||57||60||61||64||65||67||69||70||73||76))
{
SecondFunction();
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我尝试使用"或"运算符来检查数字是否等于列出的任何数字.不幸的是,这没有用.我知道我可以编码:
if(number==0||number==3||number==6....)
Run Code Online (Sandbox Code Playgroud)
我认为应该有另一种选择,是吗?
先感谢您.
因为我在循环中调用此代码.但是下面的代码给了我错误,因为document.getElementsById 它不是一个函数.我该怎么办如何在循环中调用doc.getbyid.
for (var z=1; z < i; z++){
var textbox = document.getElementsById("a"+z).value;
var textbox2 = document.getElementsById("b").value;
var textbox3 = document.getElementsById("c").value;
alert(textbox);
alert(textbox2);
alert(textbox3);
}
Run Code Online (Sandbox Code Playgroud) 我有一个功能
function test(name)
{
}
Run Code Online (Sandbox Code Playgroud)
我叫它 test("kanishka");
在一种情况下,我想将两个参数(name和age)传递给此函数:
test("kanishka",27);
Run Code Online (Sandbox Code Playgroud)
在PHP中,我可以通过将默认值设置为age:
test(name , age = NULL)
Run Code Online (Sandbox Code Playgroud)
在JavaScript中有什么方法可以做到这一点吗?
我试过了
test(name , age = NULL)
Run Code Online (Sandbox Code Playgroud)
但它会给出错误.
我可以宣布2个功能
test(name) and test(name , age = NULL)
Run Code Online (Sandbox Code Playgroud)
但它重复了代码.
或者我可以更改我之前的函数调用,它将一个参数带到两个参数,给出一个默认值age:
test("kanishka" , 0)
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我必须找到所有函数调用并更改它们.
如何获取td's的tr,其中td指数(行内)不为1或2?也就是说,我想跳过前两列.以下代码完成了工作,但是有更好的方法吗?我在想(td eq(0) || eq(1))这样的事情.
$("#time-entry tr").each(function () {
var trId = $(this).attr('id');
$(String.format("#{0} td input[id^=txt]", trId)).each(function () { //rewrite
var tdIndex = $(this).index();
if(tdIndex != 1 && tdIndex != 2) {
}
});
});
Run Code Online (Sandbox Code Playgroud) 我是backbonejs的新手.我试图将正确的this对象传递给回调函数,其中该函数是视图的方法.
我目前的解决方案
APP.LocationFormView = APP.View.extend({
initialize: function () {
if (navigator.geolocation) {
var that = this;
var success = function(position) {
_.bind(that.onSuccessUpdatePos, that, position)();
};
var error = function(error) {
_.bind(that.onFailUpdatePos, that, error)();
}
navigator.geolocation.getCurrentPosition(success,
error);
} else {
}
},
onSuccessUpdatePos: function(position) {
// We can access "this" now
},
onFailUpdatePos : function(error) {
// We can access "this" now
}
});
Run Code Online (Sandbox Code Playgroud)
这是实现我想要的正确方法吗?对此有没有更简洁的解决方案?
<ul id="footernav">
<li><a href="javascript:void(0);" id="chat" data-icon="custom" data-transition="none">Tools</a></li>
<li><a href="javascript:void(0);" id="email" data-icon="custom" data-transition="none">My Ride</a></li>
<li><a href="javascript:void(0);" id="login" data-icon="custom" data-transition="none">News</a></li>
<li><a href="javascript:void(0);" id="skull" data-icon="custom" data-transition="none">Cool</a></li>
<li><a href="javascript:void(0);" id="coffee" data-icon="custom" data-transition="none" class"ui-btn-active ui-state-persist">Contact</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
现在,我使用这个jQuery函数来获取id的li我已单击其中:
$('#footernav li').click(function(){
alert($(this).attr('id'));
});
Run Code Online (Sandbox Code Playgroud)
但它回来了undefined.
我有一个输入,我想知道我的数组中是否存在任何值.举个简单的例子,我的数组是
var _array = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
Run Code Online (Sandbox Code Playgroud)
如果我简单地输入'sun',我已经想出如何触发它,但我想知道'hhsun'或'sunee'是否也存在.
$('input').keyup(function() {
var _val = $(this).val();
$('p').text(_val);
var _array = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
if (_array.indexOf(_val) != -1) {
$('span').text('it worked');
} else {
$('span').text(_array.indexOf(_val));
}
});
Run Code Online (Sandbox Code Playgroud)
我有一个函数foo,它在循环中进行多个(并行)异步调用.我需要等到所有呼叫的结果都可用.如何foo在所有数据可用后返回完整结果,或以其他方式触发某些处理?
我尝试将每个结果添加到数组中,但是直到我需要使用它之后才会填充数组.
function foo() {
var results = [];
for (var i = 0; i < 10; i++) {
someAsyncFunction({someParam:i}, function callback(data) {
results.push(data);
});
}
return results;
}
var result = foo(); // It always ends up being an empty array at this point.
Run Code Online (Sandbox Code Playgroud)
注意:这个问题是按照现有通用"如何从异步调用返回响应?"的方式有意识地通用的.问题.这个问题有一些很好的答案,但不包括多个异步调用.还有一些其他问题提到多个调用,但我找不到任何基于循环的调用,有些只有jQuery答案等等.我希望这里有一些不依赖于特定库的通用技术.
我有几个表行和表,其中包含隐藏的输入字段和值.我需要做的是遍历所有隐藏的输入字段并找到一个特定的字段,其中class ='something'和value = something并转到<td>该输入字段并更改背景颜色.
$('input:hidden').each(function(){
if( $(this).find(".id_schedule_hours") && $(this).val() == 1) {
console.log('here')
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码,目的是定义和使用对象列表,但我得到一个'undefine' post_title.我究竟做错了什么?我不想将数组命名为对象的属性,我只想要一个对象的集合/数组.
var templates = [{ "ID": "12", "post_title": "Our Title" }
, { "ID": "14", "post_title": "pwd" }];
function templateOptionList() {
for(var t in templates) {
console.log(t.post_title);
}
}
$(function () {
templateOptionList();
});
Run Code Online (Sandbox Code Playgroud) javascript ×8
jquery ×4
arrays ×1
attr ×1
backbone.js ×1
html ×1
if-statement ×1
indexof ×1
undefined ×1