English 中文(简体)
Java: 如果有变数,如果等于价值
原标题:JavaScript : Check if variable exists and if equal to value
  • 时间:2011-11-17 21:50:47
  •  标签:
  • javascript

我有三页使用同一代码,有一页有这一变量,另两页有变票。 类型值为1或2。 我需要首先检查是否存在机票券、没有界定的票价以及第二需要确定是否为一票或二票。

如果说明产生错误:

if(typeof ticketType != undefined && ticketType == 1){}

它说的是ticket Type isn t下定义。 如果说话是为了先考虑一下,那是 t,如果说话,但火焰火力仍然产生or,那么我就把话推到che。

任何想法? 必须找到办法......

最佳回答

<>代码>未定义的在使用<>代码/代码>时,须在上面引用。

if(typeof ticketType !=  undefined  && ticketType == 1){}
问题回答

Wrap it in parenthesis.

if (typeof (ticketType)!= 未加界定和计划; 类型==1

错误信息相当清楚。 为此:

var ticketType;
if(typeof ticketType != undefined && ticketType == 1){}

我们不能仅仅提及不存在的变量。 只有在指派以下人员时,你才能这样做:

ticketType = 1;

浏览器投诉,因为<代码>tuketType是一个不为人知的识别符号。 然而,如果我们首先宣布(即使没有分配任何价值),它就会为人所知,而是以<代码>未界定的数值表示。

if(typeof ticketType !=  undefined  && ticketType == 1){}

我认为,你需要围绕未界定的字句。 至少我总是这样做。

你对未界定的变数进行不正确的核对,但如果你重新规划如何使用,为什么不仅确保加以界定?

ticketType = ticketType || 1;
if (ticketType === 1) {
  //do stuff
}

正如其他人所显示的那样,在联合材料中,检查未界定价值的标准方法是:

typeof somevar ===  undefined 




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签