0

我有一个 javascript 函数,可以检查文本框中的值,如果文本框不为空,它会输出一条语句。文本框采用数值,我想包含输出到 html 的数值。

这是html

   <br><label id="cancelphoneLabel">1-800-555-1111</label>
   <br><label id="mdamountLabel">Monthly Donation:
<td>
    <input type="text" id="mdamountBox" style="width:50px;" name="md_amt" value="" placeholder="Monthly" onkeyup="monthlycheck()" autocomplete="off">

   <br><label id="mnthlychkdiscoLabel">&nbsp;</label>

和 Javascript

 function monthlycheck() {

var mnthchk = document.getElementById("mdamountBox").innerHTML; <---i want to pass the value of this box
var cancelPhone = document.getElementById("cancelphoneLabel").innerHTML;



if (mnthchk.value != "") {

    var newHTML = "<span style='color:#24D330'> Your Monthly pledge in the amount of $<label id='dollarLabel'>&nbsp;</label> is valid and will be deducted this time every month<br> untill you notify us of its cancellation by calling <label id='cancelphonelistLabel'>&nbsp;</label> </span>";

    document.getElementById("mnthlychkdiscoLabel").innerHTML = newHTML;
    document.getElementById("cancelphonelistLabel").innerHTML = cancelPhone;
    document.getElementById("dollarLabel").innerHTML = mnthchk;   <----passed to here

我无法传递值,它只显示空白,我可以硬编码一个值并输出正常,这就是 jsfiddle 当前的样子http://jsfiddle.net/rn5HH/4/

提前致谢

4

5 回答 5

1

您的线路:

var mnthchk = document.getElementById("mdamountBox").innerHTML;

应该:

var mnthchk = document.getElementById("mdamountBox");

然后您可以像这样获取文本输入的值:

var newmnthchk = mnthchk.value;

在这里工作 JS Fiddle:http: //jsfiddle.net/rn5HH/10/

于 2013-05-14T16:22:54.657 回答
1

输入元素没有子节点,因此innerHTML为空白。如果您想读取它们的值,请使用该value属性。

于 2013-05-14T16:18:38.053 回答
0

这里的问题是你试图获取你想要值的innerHtml。从您的小提琴中,只需更改此行:

var mnthchk = document.getElementById("mdamountBox").innerHTML;

...对此:

var mnthchk = document.getElementById("mdamountBox").value;
于 2013-05-14T16:21:54.350 回答
0

检查下面的代码:

HTML 代码

<br><label id="cancelphoneLabel">1-800-555-1111</label>
<br><label id="mdamountLabel">Monthly Donation:
<td>
    <input type="text" id="mdamountBox" style="width:50px;" name="md_amt" value="" placeholder="Monthly" onkeyup="monthlycheck()" autocomplete="off">

<br><label id="mnthlychkdiscoLabel">&nbsp;</label>

Javascript代码

function monthlycheck() {

var mnthchk = document.getElementById("mdamountBox").value;
var cancelPhone = document.getElementById("cancelphoneLabel").innerHTML;



if (mnthchk.value != "") {

    var newmnthchk = '5';
    newmnthchk = mnthchk;
    var newHTML = "<span style='color:#24D330'> Your Monthly pledge in the amount of $<label id='dollarLabel'>&nbsp;</label> is valid and will be deducted this time every month<br> untill you notify us of its cancellation by calling <label id='cancelphonelistLabel'>&nbsp;</label> </span>";

    document.getElementById("mnthlychkdiscoLabel").innerHTML = newHTML;
    document.getElementById("cancelphonelistLabel").innerHTML = cancelPhone;
    document.getElementById("dollarLabel").innerHTML = newmnthchk;

}
}

这是在 Fiddle 上完美检查的工作代码,您需要使用获取 DOM 值

var mnthchk = document.getElementById("mdamountBox").value;
于 2013-05-14T16:38:13.870 回答
0

利用document.getElementById("mdamountBox").value

于 2013-05-14T16:21:03.603 回答