简体   繁体   English

在JS函数中返回值

[英]Returning values inside a JS function

Can someone explain to me why I cannot use document.getElementById('id').value inside of a function? 有人可以向我解释为什么我不能在函数内部使用document.getElementById('id')。value吗?

I was trying to make a simple multiplication script (im learning JS, its actually kinda fun) and realized quickly how annoying typing that whole line of code just to return a value is, so I wrote a small function: 我试图制作一个简单的乘法脚本(我在学习JS,实际上很有趣),并很快意识到,仅仅为了返回一个值而输入整行代码是多么烦人,所以我写了一个小函数:

<script type="text/javascript">
function value(elementid){
return document.getElementById(elementid).value
}
</script>

However, this does not function and will simply break my whole script's functionality. 但是,这不起作用,只会破坏整个脚本的功能。 I wanted to simply type value('id') to return the value of the element. 我只想键入value('id')以返回元素的值。

To fix it, a friend suggested I take out the .value in the function and add it to the end of each line where I call the function instead, like value('id').value. 为了解决这个问题,一位朋友建议我在函数中取出.value并将其添加到我调用该函数的每一行的末尾,例如value('id')。value。

Why didn't my first way work? 为什么我的第一种方法不起作用?

Thanks for the help! 谢谢您的帮助!

Change your function name to something like ' getValue '. 将函数名称更改为类似于“ getValue ”的名称。 This code works on my machine :P 这段代码在我的机器上可用:P

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<script type="text/javascript">
    function getvalue(id) {return document.getElementById(id).value;}
</script>
</head>
<body>
    <input type="text" id="Text1" />
    <input type="button" onclick="alert(getvalue('Text1'))" /> <br />

    <input type="text" id="Text2" />
    <input type="button" onclick="alert(getvalue('Text2'))" /> <br />

    <input type="text" id="Text3" />
    <input type="button" onclick="alert(getvalue('Text3'))" /> <br />

</body>
</html>

Are you sure the element that you are getting has a value property and not something else like .text or ...? 您确定要获取的元素具有value属性,而不是诸如.text或...之类的其他元素吗?

If it is an INPUT tag, you can get away with .value and some other HTML controls element also, but for regular DIV, SPAN, etc. I don't think they expose a .value property. 如果它是INPUT标记,则也可以使用.value和其他一些HTML控件元素,但是对于常规DIV,SPAN等。我认为它们没有公开.value属性。 Check the tag documentation to see if they support .value. 检查标签文档以查看它们是否支持.value。

I believe russau is on the right track. 我相信罗绍走上正轨。 You could test this by moving your <script> block to the end of the page, because hopefully everything would load by that point unless you have a complicated page structure. 您可以通过将<script>块移动到页面的末尾来进行测试,因为除非您具有复杂的页面结构,否则希望到那时所有内容都会加载。

Not every DOM element has a property called value which could be why your script is breaking. 并非每个DOM元素都有一个名为value的属性,这可能就是脚本中断的原因。 You could try the following: 您可以尝试以下方法:

<script type="text/javascript">
    function value(elementid){
        var el = document.getElementById(elementid);
        return el.value || el;
    }
</script>

this will return el.value if it exists or if it doesn't just the element. 如果存在或不只是元素,它将返回el.value。 this isnt the most elegant solution but it might stop your other scripts from breaking. 这不是最优雅的解决方案,但它可能会阻止您破坏其他脚本。 Without knowing the context in which you are calling that function, i can't help more 在不知道调用该函数的上下文的情况下,我无能为力

What you are doing should work. 您正在做的事情应该起作用。 What calls this? 这叫什么? It's possible your element isn't in the DOM when you are calling it. 调用时,您的元素可能不在DOM中。 Ie calling it in inline script before the actual element. 即在实际元素之前以内联脚本调用它。

将函数的名称从“值”更改为其他名称

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

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