admin管理员组文章数量:1399006
I have this html code:
<table border="" cellpadding="3" cellspacing="0" id="mytable">
<tbody><tr><td>Song:</td> <td><input type="text" name="song" maxlength="64" size="48" /> </td></tr>
<tr><td>Artist:</td> <td><input type="text" name="artist" maxlength="16" size="48" /> </td></tr>
How do I get the values of "song" and "artist"? I have tried:
var elTableCells = mytable.getElementsByTagName("td");
alert(elTableCells[2].value);
alert(elTableCells[4].value);
but I keep getting "undefined" even when there is text inside the textbox
I have this html code:
<table border="" cellpadding="3" cellspacing="0" id="mytable">
<tbody><tr><td>Song:</td> <td><input type="text" name="song" maxlength="64" size="48" /> </td></tr>
<tr><td>Artist:</td> <td><input type="text" name="artist" maxlength="16" size="48" /> </td></tr>
How do I get the values of "song" and "artist"? I have tried:
var elTableCells = mytable.getElementsByTagName("td");
alert(elTableCells[2].value);
alert(elTableCells[4].value);
but I keep getting "undefined" even when there is text inside the textbox
Share Improve this question edited Jul 29, 2017 at 22:40 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 18, 2011 at 21:52 Franz PayerFranz Payer 4,13716 gold badges55 silver badges78 bronze badges6 Answers
Reset to default 4First, td
elements don't have a value
attribute, so calling .value
on them won't do a thing. Second, the value is actually on the input
element, so you need to do mytable.getElementsByTagName('input')
instead. Even better would be to give your input fields ids and then use getElementById
. This would mean you could alter your HTML without your JS breaking.
The tagName that is relevant here is "input", not td. That will get you what you're looking for.
It's almost a cliche on StackOverflow, but I suggest you take a look at JQuery. JQuery allows you to treat your html as a database and query it to get this kind of stuff. If your markup was like this:
<input type="text" id="song" maxlength="64" size="48" />
You could get the value like this:
$("#id").value();
If you wanted to get the value of the maxlength attribute on it, you could do this:
$("#id").attr("maxlength");
Much more fun than digging through element arrays.
var song = document.getElementsByName("song").value;
var artist = document.getElementsByName("artist").value;
Download jquery
jQuery("input[name='song']").val()
To try and resolve the argument below. This can be down with plain javascript as above but for simplicity and cross browser standardisation I would remend jQuery. It is a widely used and generally highly remended library.
alert(document.getElementsByName("song")[0].value);
alert(document.getElementsByName("artist")[0].value);
I find this to work effectively, Below I am getting a date from an asp textbox and then getting today's date. I then send both dates through a function to get the number of months that I need to do my calculation (be aware that the code uses Infragistics libraries (if you intent to copy and paste, replace those calls with standard JavaScript calls):
function calculateMonthlyRepayment()
var tStartDate = $find('dp_Start_Date');
var startDate = tStartDate.get_value();
var today = new Date();
var numMonths = monthDiff(startDate, today);
var rate = igedit_getById('WebNumericEdit_InterestRate').getValue() * 0.01;
var intRate = (rate / 100) / 12;
var principal = igedit_getById('WebCurrencyEdit_Proposed_Owing').getValue();
var repayment = (principal * (Math.pow((1 + intRate), numMonths)) * intRate / (Math.pow((1 + intRate), numMonths) - 1));
igedit_getById('txt_Monthly_Repayment').setValue(repayment);
}
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
本文标签: htmlGetting the values of textboxes in JavascriptStack Overflow
版权声明:本文标题:html - Getting the values of textboxes in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744198334a2594845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论