javascript 将值设置为 getElementsByName 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14895044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:52:13  来源:igfitidea点击:

set value to array of getElementsByName

javascriptdynamicgetelementsbyname

提问by TJ Smith

I am having the following situation:

我有以下情况:

  • I must use dynamic table (add/remove rows) - code from http://viralpatel.net/blogs/dynamically-add-remove-rows-in-html-table-using-javascript/
  • 我必须使用动态表(添加/删除行) - 来自http://viralpatel.net/blogs/dynamically-add-remove-rows-in-html-table-using-javascript/ 的代码
  • rows contain html input boxes with name, no id
  • 行包含带有名称的 html 输入框,没有 id
  • I need to be able to set values to these boxes

    I have tried the following, but get into syntax problem:

    <!-- html part -> this row will be replicated by the dynamic table code -->

    <tr><td><input type=input name=mybox></td></tr>
    
    //js part - variant 1:
    document.getElementsByName("mybox").item(j).value = j;
    
    //js part - variant 2:
    document.getElementsByName("mybox")[j].setAttribute("value", j);
    

    None of these seems to work. Can you suggest a right way to do it?

    Thanks!

  • 我需要能够为这些框设置值

    我尝试了以下方法,但遇到了语法问题:

    <!-- html部分->该行会被动态表代码复制-->

    <tr><td><input type=input name=mybox></td></tr>
    
    //js part - variant 1:
    document.getElementsByName("mybox").item(j).value = j;
    
    //js part - variant 2:
    document.getElementsByName("mybox")[j].setAttribute("value", j);
    

    这些似乎都不起作用。你能建议一个正确的方法吗?

    谢谢!

  • 采纳答案by d4rkpr1nc3

    document.getElementsByName("mybox")[j].value = j;

    document.getElementsByName("mybox")[j].value = j;

    回答by marekful

    getElementsByNamereturns an array of HTMLElements.

    getElementsByName返回一个 HTMLElements 数组。

    This line has the correct syntax but I doubt j, the value you are trying to set is the right index value of the returned array.

    此行具有正确的语法,但我怀疑j,您尝试设置的值是返回数组的正确索引值。

    document.getElementsByName("mybox")[j].setAttribute("value", j);
    

    The fist occurrence of jshould be the index of the returned array. If It's the first element found by the given name then 0, if the 2nd, then 1, etc.

    第一次出现的j应该是返回数组的索引。如果它是给定名称找到的第一个元素,则为 0,如果是第二个,则为 1,依此类推。

    回答by Jefferson Henrique C. Soares

    Your html code seems wrong, try put the double quotes:

    您的 html 代码似乎有误,请尝试使用双引号:

    <tr><td><input type="input" name="mybox"></td></tr>
    

    回答by Mike

    We don't know what is the value of jand how it's being set.

    我们不知道它的价值是什么j以及它是如何设置的。

    First variant should work. Just set a value that makes sense. For testing purposes: document.getElementsByName("mybox")[3].value = "Test";

    第一个变体应该可以工作。只需设置一个有意义的值。出于测试目的:document.getElementsByName("mybox")[3].value = "Test";

    Also use quotes for attributes type="input"and name="mybox"

    还使用引号作为属性type="input"name="mybox"

    回答by Anna.P

    try this $("#mybox").eq(j).val("your value");

    试试这个 $("#mybox").eq(j).val("your value");

    回答by TJ Smith

    thank you for the feedback.

    感谢您的反馈。

    Michal's solution is the one I got working.

    Michal 的解决方案是我得到的解决方案。

    document.getElementsByName("mybox")[3].value = "Test";
    

    = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

    = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

    However I tried my 2nd variant as suggested by Marcell Fül?p in a very rudimentary way and saw some strange behaviour:

    然而,我按照 Marcell Fül?p 的建议,以一种非常基本的方式尝试了我的第二个变体,并看到了一些奇怪的行为:

    //manual assignment of indexes
    document.getElementsByName("mybox")[0].setAttribute("value",0);
    document.getElementsByName("mybox")[1].setAttribute("value",1);
    document.getElementsByName("mybox")[2].setAttribute("value",2);
    

    The strange result was only the 1st box got it's value. I'd be happy to understand why...

    奇怪的结果是只有第一个盒子得到了它的价值。我很乐意理解为什么...