简体   繁体   English

Javascript函数没有返回值

[英]Javascript function not returning a value

I have a javascript function that check for a value from a text box and the if the text box is not blank it outputs a statement. 我有一个javascript函数,检查文本框中的值,如果文本框不是空白,它输出一个语句。 The text box take a numeric value, i want to include that numeric value that is output to html. 文本框采用数值,我想包括输出到html的数值。

here is the 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>

and the Javascript 和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

i cant get the value passed, it only shows blank, i can hardcode a value and will output fine, which is how the jsfiddle is currently http://jsfiddle.net/rn5HH/4/ 我无法获得传递的值,它只显示空白,我可以硬编码一个值并输出正常,这就是jsfiddle目前的方式http://jsfiddle.net/rn5HH/4/

thanks in advance 提前致谢

Input elements don't have child nodes, therefore innerHTML is blank. 输入元素没有子节点,因此innerHTML是空白的。 If you want to read their value, use the value property. 如果要读取其值,请使用value属性。

Your line: 你的路线:

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

Should be: 应该:

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

Then you can get the value of the text input like this: 然后你可以得到文本输入的值,如下所示:

var newmnthchk = mnthchk.value;

Working JS Fiddle here: http://jsfiddle.net/rn5HH/10/ 在这里工作JS小提琴: http//jsfiddle.net/rn5HH/10/

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

The problem here is you are trying to get the innerHtml, where you want the value. 这里的问题是你想要获取innerHtml,你想要的值。 From your fiddle, just change this line: 从你的小提琴,只需改变这一行:

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

...to this: ......对此:

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

check below code : 检查以下代码:

HTML Code 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 Code 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;

}
}

This is working code perfectly checked on Fiddle ,you need to get DOM value using 这是在Fiddle上完美检查的工作代码,您需要使用获取DOM值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM