获取HTML值以与JavaScript值相关联

时间:2017-07-28 21:24:57

标签: javascript html select

我正在尝试构建5个选择框。每个框的值将改变最底部的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值?

2 个答案:

答案 0 :(得分:0)

您可以在选择中使用onchange事件。只需将<select>更改为<select onchange="cc(value)">,每次选择新值时,都会使用当前值调用函数cc

您可以查看此here

答案 1 :(得分:0)

['learn','study']
[['buy', 'purchase'], ['buy', 'purchase'], ['learn', 'study'], ['learn', 'study'], ['learn', 'study'], ['learn', 'study']]