在javascript中传递document.getElementByid中的变量 [英] Pass variable in document.getElementByid in javascript

查看:794
本文介绍了在javascript中传递document.getElementByid中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个账号可以储存的可变账号。现在我想获取id为account_number的元素的值。如何在JavaScript中做到这一点?



我试过做document.getElementById(account_number).value,但它是空的。



html看起来像这:

 < input class ='transparent'disabled type ='text'name ='113114234567_name'id ='113114234567_name' value ='Neeloy'style ='border:0px; height:25px; font-size:16px; line-height:25px;'/> 

和js是:

  function getElement()
{
var acc_list = document.forms.editBeneficiary.elements.bene_account_number_edit;

for(var i = 0; i< acc_list.length; i ++)
{
if(acc_list [i] .checked == true)
{

var account_number = acc_list [i] .value.toString();
var ben_name = account_number +_name;

alert(document.getElementById('+ ben_name.toString()+').value);
}
}

}



这里bene_account_number_edit是单选按钮。



谢谢

解决方案

您是否仅存储一个整数作为元素的 id 属性?如果是这样,浏览器在用整数ID查找元素时倾向于采用奇怪的方式。尝试传递 account_number.toString(),而不是。

如果这样做不起作用,请将account_ 到元素的开头 id 属性,然后调用 document.getElementById('account_'+ account_number).value


I have a variable account_number in which account number is stored. now i want to get the value of the element having id as account_number. How to do it in javascript ?

I tried doing document.getElementById(account_number).value, but it is null.

html looks like this :

<input class='transparent' disabled type='text' name='113114234567_name' id='113114234567_name' value = 'Neeloy' style='border:0px;height:25px;font-size:16px;line-height:25px;' />

and the js is :

function getElement()
{   
 var acc_list = document.forms.editBeneficiary.elements.bene_account_number_edit;

    for(var i=0;i<acc_list.length;i++)
        {
            if(acc_list[i].checked == true)
                {

                    var account_number = acc_list[i].value.toString();
                    var ben_name = account_number + "_name";

                    alert(document.getElementById("'" + ben_name.toString() + "'").value);
                }
         }

}

here bene_account_number_edit are the radio buttons.

Thanks

解决方案

Are you storing just an integer as the element's id attribute? If so, browsers tend to behave in strange ways when looking for an element by an integer id. Try passing account_number.toString(), instead.

If that doesn't work, prepend something like "account_" to the beginning of your elements' id attributes and then call document.getElementById('account_' + account_number).value.

这篇关于在javascript中传递document.getElementByid中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆