我试图从特定形式获取元素的值。因为FOREACH我有100个表单..我可以获取Form name = 1表单名称= 10但我无法从特定表单中获取隐藏值。谁能帮我?感谢。
[% FOREACH k_bilgi IN k_bilgiler %]
<form name='[% k_bilgi.i%]'>
<input type="hidden" name="itemnumberi" value="[% k_bilgi.itemnumber %]" />
<input type="hidden" name="returndate" value="[% k_bilgi.returndate %]" />
<input type="hidden" name="title" value="[% k_bilgi.title %]" />
<input type="hidden" id="author" name="author" value="[% k_bilgi.author %]" />
<input type="hidden" name="branchname" value="[% k_bilgi.branchname %]" />
</form>
[% END %]
我的javascript代码(b将是1或10或40)
function gonderildi(b) {
var div1 = document.forms[i];
alert(div1) // this works.
var a = document.forms[i].elements['itemnumberi'];
alert(a); // this is not work..
答案 0 :(得分:1)
document.forms[i].querySelector("input[name='itemnumberi']")
答案 1 :(得分:1)
如果使用id属性,它会更简单,更强大。 ID的查询速度更快。 您可以像这样更改代码:
[% FOREACH k_bilgi IN k_bilgiler %]
<form name='[% k_bilgi.i%]'>
<input type="hidden" id="itemnumberi_[% k_bilgi.i%]" name="itemnumberi" value="[% k_bilgi.itemnumber %]" />
</form>
[% END %]
function gonderildi(b) {
var div1 = document.forms[i];
var a = document.forms[i].getElementById('itemnumberi_' + i);
答案 2 :(得分:0)
我想,你需要传递索引..试试这个。
function gonderildi(b) {
var div1 = document.forms[i];
alert(div1) // this works.
var a = document.forms[i].elements[0];
alert(a); // It should work ..