2017-07-28 61 views
0

我試圖構建5個選擇框。每個盒子上的值會改變底部的JavaScript「卡片值」。這將全部更新「計數」,我應該下注表達。獲取HTML值以關聯到JavaScript值

https://jsbin.com/vasaqej/edit?html,js,output

var count = 0; 
 
var shouldIBet = ""; 
 

 

 
function cc(card) { 
 
    switch(card) { 
 
    case 2: 
 
    case 3: 
 
    case 4: 
 
    case 5: 
 
     count++ 
 
     break; 
 
    case 10: 
 
    case "J": 
 
    case "Q": 
 
    case "K": 
 
    case "A": 
 
     count-- 
 
     break; 
 
    } 
 
    if (count >0) { 
 
    return shouldIBet = " Bet!"; 
 
    } else { 
 
    return shouldIBet = " Hold!"; 
 
    } 
 

 
    return "enter something else" 
 
} 
 
cc(2); cc(3); cc(4); cc(5); cc(5); 
 

 
document.write(count + shouldIBet)
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
</head> 
 
<body> 
 
<select> 
 
    <option value = 2>2</option> 
 
    <option value = 3>3</option> 
 
    <option value = 4>4</option> 
 
    <option value = 5>5</option> 
 
    <option value = 6>6</option> 
 
    <option value = 7>7</option> 
 
    <option value = 8>8</option> 
 
    <option value = 9>9</option> 
 
    <option value = 10>10</option> 
 
    <option value = "J">J</option> 
 
    <option value = "Q">Q</option> 
 
    <option value = "K">K</option> 
 
    <option value = "A">A</option> 
 
</select> 
 
</body> 
 
</html>

如何獲得HTML選擇的值傳遞給JavaScript值?

回答

0

您可以在您的選擇中使用onchange事件。只需將<select>更改爲<select onchange="cc(value)">,並且每次選擇新值時,功能cc都將以當前值調用。

您可以在此here

+0

嗯看看......也許我不是太清楚......目前,我有一個選擇列表。應該有五個,但我寧願只是得到一個工作...在JavaScript的底部有五個值「cc」我試圖鏈接每個選擇標記的值來更新一個「cc」值 –

0

function myFunction(){ 
 
var e = document.getElementById("btnselect"); 
 
var boxvalue = e.options[e.selectedIndex].value; 
 
alert(boxvalue); 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
</head> 
 
<body> 
 
<select id="btnselect" onchange="myFunction();"> 
 
    <option value = 2>2</option> 
 
    <option value = 3>3</option> 
 
    <option value = 4>4</option> 
 
    <option value = 5>5</option> 
 
    <option value = 6>6</option> 
 
    <option value = 7>7</option> 
 
    <option value = 8>8</option> 
 
    <option value = 9>9</option> 
 
    <option value = 10>10</option> 
 
    <option value = "J">J</option> 
 
    <option value = "Q">Q</option> 
 
    <option value = "K">K</option> 
 
    <option value = "A">A</option> 
 
</select> 
 
</body> 
 
</html>