我有一个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"> </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'> </label> is valid and will be deducted this time every month<br> untill you notify us of its cancellation by calling <label id='cancelphonelistLabel'> </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/
提前致谢
答案 0 :(得分:1)
输入元素没有子节点,因此innerHTML
为空。如果要阅读其值,请使用value
属性。
答案 1 :(得分:1)
你的专栏:
var mnthchk = document.getElementById("mdamountBox").innerHTML;
应该是:
var mnthchk = document.getElementById("mdamountBox");
然后你可以像这样得到文本输入的值:
var newmnthchk = mnthchk.value;
在这里工作JS小提琴:http://jsfiddle.net/rn5HH/10/
答案 2 :(得分:0)
使用document.getElementById("mdamountBox").value
答案 3 :(得分:0)
这里的问题是你想要获取innerHtml,你想要的值。从你的小提琴,只需改变这一行:
var mnthchk = document.getElementById("mdamountBox").innerHTML;
......对此:
var mnthchk = document.getElementById("mdamountBox").value;
答案 4 :(得分: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"> </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'> </label> is valid and will be deducted this time every month<br> untill you notify us of its cancellation by calling <label id='cancelphonelistLabel'> </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;