JavaScript字数 - 计算

时间:2014-05-29 07:07:18

标签: javascript

我有一个脚本来计算附件中的单词是否正常。

function loadDemo() {

      WordCount.words("file_attach", function(words) {
        document.getElementById("words").innerText = words;
      });


    var elt = document.getElementById("words");
    var words = elt.options[elt.selectedIndex].value;

    words = parseInt(words);

    var tprice = (words /500) * 12;

    document.getElementById("tprice").innerText=tprice;

}

然后,我想要计算在下面的范围框中打印的价格。

字数打印到其跨度,但价格不是。我的计算在哪里出错?

<div id="result"></div>
<label for="name"><span>Name</span>
<input type="text" name="name" id="name" placeholder="Enter Your Name" />
</label>

<label for="email"><span>Email Address</span>
<input type="email" name="email" id="email" placeholder="Enter Your Email" />
</label>

<label for="file"><span>Attachment</span>
<input type="file" name="file_attach" id="file_attach" onChange="loadDemo()" />
</label>

<label for="words"><span>No. of Words</span>
<span id="words"></span>
</label>

<label for="tprice"><span>Price</span>
<span id="tprice"></span>
</label>

<label for="message"><span>Message</span>
<textarea name="message" id="message" placeholder="Enter Your Name"></textarea>
</label>

<label><span>&nbsp;</span>
<button class="submit_btn" id="submit_btn">Submit</button>
</label>

以上是表格的HTML。

由于

1 个答案:

答案 0 :(得分:0)

用于查找单词的JavaScript是:

var elt = document.getElementById("words");
var words = elt.options[elt.selectedIndex].value;

但第二行是您用于从选择下拉列表中检索值的内容。但是元素<span id="words"></span>不是选择元素。你需要使用:

var elt = document.getElementById("words");
var words = elt.textContent;

Demo

作为旁注,将<label>标记用于不构成控件的内容有点不寻常。具体而言,使用标签的for属性指向非输入元素是没有意义的,因为它们无法获得焦点。