English 中文(简体)
2. 尊重两个不同的价值观,改变一个可变的动态
原标题:Array equal two different values and change a variable dynamically

我想要一个用户在一份ID号码中钥匙。 当用户点击一个纽州时,该代码将寻找一个阵列,列出所有id号清单,以核实是否存在。 然后将检查该数字的价格。 根据价格和所研究的国际发展指数数字,我希望能动态地改变一个称为成本的变量。 例如,“55555555”号中的用户钥匙。 如果第55555号身份证存在的话,该法典将检查该补贴的价格。 基于这一价格,我希望它改变一种可变的所谓成本。 同样,如果我看不到“1234”。 如果存在,则会看看病面价,然后改变可变的费用。

我甚至不知道从哪里开始。 我正在考虑使用阵列来绘制粗数和价格图,但我不知道这是否会奏效。 我想有一些数字基本上等于另一个数字,然后根据第二数字改变一个变量,我可以想到如何这样做。

id[0] = new Array(2)
id[1] = "5555";
id[2] = "6789";
price = new Array(2)
price[0] = 45;
price[1] = 18;
问题回答

您可以把物体当作像物体一样的字典。

// Default val for cost
var cost = -1;

// Create your dictionary (key/value pairs)
// "key": value (e.g. The key "5555" maps to the value  45 )
var list = {
    "5555": 45,
    "6789": 18
};

// jQuery click event wiring (not relevant to the question)
$("#yourButton").click(function() {
    // Get the value of the input field with the id  yourInput  (this is done with jQuery)
    var input = $("#yourInput").val();

    // If the list has a key that matches what the user typed,
    // set `cost` to its value, otherwise, set it to negative one.
    // This is shorthand syntax. See below for its equivalent
    cost = list[input] || -1;

    // Above is equivalent to
    /*
    if (list[input])
        cost = list[input];
    else
        cost = -1;
    */

    // Log the value of cost to the console
    console.log(cost);
});




相关问题
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.

热门标签