首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用select标记中的选项作为Javascript的输入

使用select标记中的选项作为Javascript的输入
EN

Stack Overflow用户
提问于 2015-08-03 13:36:25
回答 2查看 49关注 0票数 0

到目前为止,我已经编写了以下代码:

代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body background="Pics\Pattern.png">
<body>
<table style="width:10%">
<tr>
<th > <font family = "Verdana" color = #fff>Current Stat</font></th>
<th > <font family = "Verdana" color = #fff>Current Level</font></th>
<th > <font family = "Verdana" color = #fff> Base Stat</font></th>
<th > <font family = "Verdana" color = #fff> EV's In Stat</font></th>
<th > <font family = "Verdana" color = #fff> What Stat</font></th>
<th ><font family = "Verdana" color = #fff> Nature Is:</font></th>
</tr>
<tr>
<td class="mycell">
<input type = "text" style="width:133px" id = "C-Stat" />
</td>
<td class="mycell">
<input type = "text" style="width:133px" id = "C-Level" />
</td>
<td class="mycell">
<input type = "text" style="width:133px" id = "B-Stat" />
</td>
<td class="mycell">
<input type = "text" style="width:133px" id = "EV-Stat" />
</td>
<td class="mycell">
<style="width:133px" />
<select  id = "What-Stat">
<option value="HP">HP</option>
<option value="Attack">Attack</option>
<option value="Defence">Defence</option>
<option value="Special Attack">Special Attack</option>
<option value="Special Defence">Special Defence</option>
<option value="Speed">Speed</option>
</select>
</td>
<td class="mycell">
<style="width:133px"/>
<select id = "Nature">
<option value="Beneficial">Beneficial</option>
<option value="Neutral">Neutral</option>
<option value="Detrimental">Detrimental</option>
</select>
</td>
</table>
<button onclick="IVFunction()">Get IV</button>
<p id="checkar"></p>
</body>
<script>
function IVFunction() {
    var CS = parseInt(document.getElementById("C-Stat").value);
    var CL = parseInt(document.getElementById("C-Level").value);
    var BS = parseInt(document.getElementById("B-Stat").value);
    var ES = parseInt(document.getElementById("EV-Stat").value);
    var N = parseInt(document.getElementById("Nature").value);
    var WS = parseInt(document.getElementById("What-Stat").value);
    var done = "Please Enter A Valid Input";

    if (N=="Beneficial") {
        var N = 1.1;
    }
    else if (N=="Neutral") {
        var N = 1.0;
    }
    else if (N=="Detrimental") {
        var N = 0.9;
    }

    if (WS == "HP") {
        var HPIV1 = ((CS-10)*100)/CL;
        var HPIV2 = HPIV1 - (2*BS) - (ES/4) - 100;
        var done = HPIV2;
    }
    else if (WS != "HP") {
        var IV1 = ((CS/N - 5)*100)/CL;
        var IV2 = IV1 - (2*BS) - (ES/4);
        var done = IV2;
    }
    document.getElementById("checkar").innerHTML = done;
}
</script>
</html>

代码向用户显示一个表,然后接受文本输入和选择框输入来运行脚本代码。我想在代码中修复的是select选项。在脚本部分中,我概述了将根据用户选择哪些选项作为输入而发生的一些条件,但是我不确定如何实际使用这些值作为脚本代码的输入;特别是关于设置N的值的部分。

如能提供任何帮助,将不胜感激:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-03 13:56:07

您不应该将parseInt()用于NWS,代码中有许多变量的重新定义。更重要的是,将所有的样式都放在css文件中。

代码语言:javascript
复制
function IVFunction() {
  var CS = parseInt(document.getElementById("C-Stat").value);
  var CL = parseInt(document.getElementById("C-Level").value);
  var BS = parseInt(document.getElementById("B-Stat").value);
  var ES = parseInt(document.getElementById("EV-Stat").value);
  var N = document.getElementById("Nature").value;
  var WS = document.getElementById("What-Stat").value;
  var done = "Please Enter A Valid Input";

  if (N == "Beneficial") {
    N = 1.1;
  } else if (N == "Neutral") {
    N = 1.0;
  } else if (N == "Detrimental") {
    N = 0.9;
  }

  if (WS == "HP") {
    var HPIV1 = ((CS - 10) * 100) / CL;
    var HPIV2 = HPIV1 - (2 * BS) - (ES / 4) - 100;
    done = HPIV2;
  } else if (WS != "HP") {
    var IV1 = ((CS / N - 5) * 100) / CL;
    var IV2 = IV1 - (2 * BS) - (ES / 4);
    done = IV2;
  }

  document.getElementById("checkar").innerHTML = done;
}
代码语言:javascript
复制
body {
  font-family: "Verdana", serif;
  background: url("..\Pics\Pattern.png");
}
th {
  color: black;
}
td {
  width: 133px
}
代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
</head>

<body>
  <table>
    <tr>
      <th>Current Stat</th>
      <th>Current Level</th>
      <th>Base Stat</th>
      <th>EV's In Stat</th>
      <th>What Stat</th>
      <th>Nature Is</th>
    </tr>
    <tr>
      <td class="mycell">
        <input type="text" id="C-Stat" />
      </td>
      <td class="mycell">
        <input type="text" id="C-Level" />
      </td>
      <td class="mycell">
        <input type="text" id="B-Stat" />
      </td>
      <td class="mycell">
        <input type="text" id="EV-Stat" />
      </td>
      <td class="mycell">
        <select id="What-Stat">
          <option value="HP">HP</option>
          <option value="Attack">Attack</option>
          <option value="Defence">Defence</option>
          <option value="Special Attack">Special Attack</option>
          <option value="Special Defence">Special Defence</option>
          <option value="Speed">Speed</option>
        </select>
      </td>
      <td class="mycell">
        <select id="Nature">
          <option value="Beneficial">Beneficial</option>
          <option value="Neutral">Neutral</option>
          <option value="Detrimental">Detrimental</option>
        </select>
      </td>
  </table>
  <button onclick="IVFunction()">Get IV</button>
  <p id="checkar"></p>
</body>

票数 1
EN

Stack Overflow用户

发布于 2015-08-03 13:44:44

你的问题是这两行:

代码语言:javascript
复制
var N = parseInt(document.getElementById("Nature").value);
var WS = parseInt(document.getElementById("What-Stat").value);

尝试将单词解析为整数的地方。

删除parseInt(),您应该可以进行如下操作:

代码语言:javascript
复制
function IVFunction() {
    var CS = parseInt(document.getElementById("C-Stat").value);
    var CL = parseInt(document.getElementById("C-Level").value);
    var BS = parseInt(document.getElementById("B-Stat").value);
    var ES = parseInt(document.getElementById("EV-Stat").value);
    var N = document.getElementById("Nature").value;
    var WS = document.getElementById("What-Stat").value;
    var done = "Please Enter A Valid Input";

    if (N == "Beneficial") {
        var N = 1.1;
    } else if (N == "Neutral") {
        var N = 1.0;
    } else if (N == "Detrimental") {
        var N = 0.9;
    }

    if (WS == "HP") {
        var HPIV1 = ((CS - 10) * 100) / CL;
        var HPIV2 = HPIV1 - (2 * BS) - (ES / 4) - 100;
        var done = HPIV2;
    } else if (WS != "HP") {
        var IV1 = ((CS / N - 5) * 100) / CL;
        var IV2 = IV1 - (2 * BS) - (ES / 4);
        var done = IV2;
    }
    document.getElementById("checkar").innerHTML = done;
}
代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
    </head>
    
    <body background="Pics\Pattern.png">
        
        <body>
            <table style="width:10%">
                <tr>
                    <th> <font family="Verdana" color=# fff>Current Stat</font>
                    </th>
                    <th> <font family="Verdana" color=# fff>Current Level</font>
                    </th>
                    <th> <font family="Verdana" color=# fff> Base Stat</font>
                    </th>
                    <th> <font family="Verdana" color=# fff> EV's In Stat</font>
                    </th>
                    <th> <font family="Verdana" color=# fff> What Stat</font>
                    </th>
                    <th><font family="Verdana" color=# fff> Nature Is:</font>
                    </th>
                </tr>
                <tr>
                    <td class="mycell">
                        <input type="text" style="width:133px" id="C-Stat" />
                    </td>
                    <td class="mycell">
                        <input type="text" style="width:133px" id="C-Level" />
                    </td>
                    <td class="mycell">
                        <input type="text" style="width:133px" id="B-Stat" />
                    </td>
                    <td class="mycell">
                        <input type="text" style="width:133px" id="EV-Stat" />
                    </td>
                    <td class="mycell">
                        <style="width:133px" />
                        <select id="What-Stat">
                            <option value="HP">HP</option>
                            <option value="Attack">Attack</option>
                            <option value="Defence">Defence</option>
                            <option value="Special Attack">Special Attack</option>
                            <option value="Special Defence">Special Defence</option>
                            <option value="Speed">Speed</option>
                        </select>
                    </td>
                    <td class="mycell">
                        <style="width:133px" />
                        <select id="Nature">
                            <option value="Beneficial">Beneficial</option>
                            <option value="Neutral">Neutral</option>
                            <option value="Detrimental">Detrimental</option>
                        </select>
                    </td>
            </table>
            <button onclick="IVFunction()">Get IV</button>
            <p id="checkar"></p>
        </body>

</html>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31788823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档