【问题标题】:HTML checkbox input changes to type "true" or "false" when used in JavaScript function在 JavaScript 函数中使用时,HTML 复选框输入更改为类型“true”或“false”
【发布时间】:2016-09-03 06:27:55
【问题描述】:

我正在尝试构建一个用于实验单元大小的计算器,允许用户选择要包含的选项。为了返回单元格的数量/大小,我构建了一个 JavaScript 函数,该函数通过输入标签上的名称字段计算已检查项目的数量。

该功能似乎工作正常,但包含它会产生意想不到的结果,即会将复选框转换为静态值字段,显示每个输入的值标签的内容(单个复选框的引用而不是系列)。

我想要(并且正在得到)这个:

相反,我现在得到了这个:

代码如下。谢谢。

function updateresults() {
  var cb_counts = {};
  var inputs = document.getElementsByTagName('input');
  for (var i = 0; i < inputs.length; i++) {
    var ip = inputs[i];
    if (ip.type = 'checkbox' && ip.checked) {
      if (cb_counts[ip.name]) {
        cb_counts[ip.name] ++;
      } else {
        (cb_counts[ip.name] = 1);
      }
    }
  }
  var mincells = cb_counts["reltype"] * cb_counts["relqual"] * cb_counts["ask"] * cb_counts["cause"] * cb_counts["receff"] * cb_counts["retone"] * cb_counts["doneff"];
  document.getElementById("mc").innerHTML = mincells;
  var samplen = document.getElementById("samps").value;
  var percell = samplen / mincells;
  document.getElementById("pc").innerHTML = percell;
}
 <h1>Experimental Cell Size Calculator</h1>
<div name="metadata">
  Author: Nathaniel D. Porter
  <br>Revised: September 2, 2016
  <br>
  <em>Please contact the <a href="mailto://nathanield.porter@gmail.com">author</a> for permission to use or modify</em>
  <br>
</div>
<div name="directions">
  Check the boxes for each option to include in collection of the following experimental manipulations. Checking only one box effectively removes the manipulation so that all participants receive the checked version.
  <br>To calculate the cell size for a specific interaction, select the question names to interact. Checking all interactions shows the size of minimal cells. This will not change the option selections above. As you change the options, the output will automatically
  update below.
</div>
<div name="inputDiv">
  <p>
    Total Sample Size:
    <input type="number" name="samplesize" id="samps" value="1000" step="1">
  </p>
  <p>
    Relationship to Donor:
    <br>
    <input type="checkbox" name="reltype" value="Parent" checked>Parent
    <br>
    <input type="checkbox" name="reltype" value="Sibling">Sibling
    <br>
    <input type="checkbox" name="reltype" value="Aunt">Aunt
    <br>
    <input type="checkbox" name="reltype" value="Colleague">Co-worker
    <br>
  </p>
  <p>
    Quality of Relationship:
    <br>
    <input type="checkbox" name="relqual" value="Random">Random
    <br>
    <input type="checkbox" name="relqual" value="Important" checked>Important Matters
    <br>
  </p>
  <p>
    Type of discovery:
    <br>
    <input type="checkbox" name="ask" value="recAsk">Asked directly by recipient
    <br>
    <input type="checkbox" name="ask" value="heard" checked>Overheard through someone else
    <br>
  </p>
  <p>
    Implied cause of need:
    <br>
    <input type="checkbox" name="cause" value="none" checked>None stated
    <br>
    <input type="checkbox" name="cause" value="nofault">Genetic (no fault)
    <br>
    <input type="checkbox" name="cause" value="fault">Lifestyle (fault)
    <br>
  </p>
  <p>
    Stated effect on recipient:
    <br>
    <input type="checkbox" name="receff" value="longer" checked>Live longer
    <br>
    <input type="checkbox" name="receff" value="better">Live better
    <br>
  </p>
  <p>
    Tone of stated effect on recipient:
    <br>
    <input type="checkbox" name="retone" value="neg" checked>Prevent negative (not die/less pain)
    <br>
    <input type="checkbox" name="retone" value="pos">Enable positive (longer life/fuller life)
    <br>
  </p>
  <p>
    Stated effect on donor:
    <br>
    <input type="checkbox" name="doneff" value="none" checked>None given
    <br>
    <input type="checkbox" name="doneff" value="pos">Benefits
    <br>
    <input type="checkbox" name="doneff" value="neg">Costs
    <br>
    <input type="checkbox" name="doneff" value="both">Both
    <br>
  </p>
  Interaction of interest:
  <br>
  <input type="checkbox" name="interaction" value="reltype" checked>Relationship to Donor
  <br>
  <input type="checkbox" name="interaction" value="relqual" checked>Quality of Relationship
  <br>
  <input type="checkbox" name="interaction" value="ask" checked>Type of discovery
  <br>
  <input type="checkbox" name="interaction" value="cause" checked>Implied cause of need
  <br>
  <input type="checkbox" name="interaction" value="receff" checked>Stated effect on recipient
  <br>
  <input type="checkbox" name="interaction" value="retone" checked>Tone of stated effect on recipient
  <br>
  <input type="checkbox" name="interaction" value="doneff" checked>Stated effect on donor
  <br>
</div>
<div name="results">
  <p>
    Number of minimal cells:
    <p id="mc"></p>
    Minimal cell size:
    <p id="pc"></p>
    <br>Number of cells in interaction: Pending
    <br>Interacted cell size: Pending
</div>

【问题讨论】:

  • 错字: ip.type = 'checkbox' &amp;&amp; ip.checked 应该是 ip.type === 'checkbox' &amp;&amp; ip.checked
  • 因为赋值而不是比较,你基本上是在做ip.type = ip.checked,它拒绝了那个值并替换了"text"

标签: javascript html checkbox input


【解决方案1】:

这只是代码中的一个典型错字:

if (ip.type = 'checkbox'  ...

顺其自然:

if (ip.type == 'checkbox' ...

必须注意,您的代码实际设置是这样的:

if (ip.type = ('checkbox' && ip.checked))

IE:

if (ip.type = <boolean>)

I.E.:无论是真还是假,它都是 type 属性的无效值,因此浏览器将其解释为默认输入类型 (TEXT)。

【讨论】:

  • 当然。我应该抓住它。该功能的那部分是从另一个答案中借来的,我检查得不够仔细。感谢您清晰完整的解释(也感谢@squint)
猜你喜欢
  • 2019-02-05
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2012-12-10
相关资源
最近更新 更多