Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将值设置为具有相同名称的文本框_Javascript_Php_Jquery_Textbox - Fatal编程技术网

Javascript 将值设置为具有相同名称的文本框

Javascript 将值设置为具有相同名称的文本框,javascript,php,jquery,textbox,Javascript,Php,Jquery,Textbox,我有很多文本框 <td><input type="text" class="textbox" name="txtname[]" id="txtname[]" /></td> <td><input type="text" class="textbox" name="txtfname[]" id= "txtfname[]" /></td> <td><input type="text" class="tex

我有很多文本框

   <td><input type="text" class="textbox" name="txtname[]" id="txtname[]" /></td>
<td><input type="text" class="textbox" name="txtfname[]" id= "txtfname[]" /></td>
<td><input type="text" class="textbox" name="txtmname[]" id="txtmname[]" /></td>

所以我可以用一个类似表格的表单来输入这些文本框的行数。我需要为这些文本框设置值。我有需要设置的行数。但是我如何才能让这些文本框中的一行使用id设置值


这是我的表格。行数是动态的,我就是这样命名的。我需要为一个带圆圈的文本框设置一个值。我该怎么做?

您的问题确实很不清楚,但我假设您只想能够管理具有动态id的组件

这不是一个问题,因为您可以简单地为您的id使用一个变量(它必须是末尾的字符串),但请记住,您的id必须保持唯一

您的算法可能是:

for(i=0;i<txtname.length;i++) {
form+="<td><input type='text' class='textbox' name='txtname' id='txtname"+i+"' /></td>"
}
for(i=0;i根据规则,“Id”在当前页面中应该是唯一的,
因此,对于不止一个DOM元素,必须使用“类”。
所以你可以这样做:

var row =$(".textbox") 
row[0].value="FirstName"
row[1].value="MiddleName";
row[2].value="lastName"
如果有4行和动态(多)列:

要访问值,请使用行[列索引][行索引]; 例如,行[0][2]。值为您提供MiddleName输入字段值


我希望您能得到这个简短的解释

正如许多人所说,html中的id属性必须是唯一的,因此此处使用数组名称不合适,请参见此处

由于这是动态生成的表列,因此我建议在表上使用命名约定,然后使用该约定查找所需的当前输入。我不知道如何访问此信息,但下面的示例就是这样做的

找到我们正在使用的桌子

在注释“$”(“#textbox[0]”)中获取所需索引的行(不确定如何确定)。val();”此索引将是0

在该行中,按名称查找输入并获取其值

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>title</title>
  </head>
  <body>
    <table id="datesheet">
        <tr>
            <td><input type="text" class="textbox" name="txtname[]" value="test0"/></td>
            <td><input type="text" class="textbox" name="txtfname[]"/></td>
            <td><input type="text" class="textbox" name="txtmname[]"/></td>
        </tr>
        <tr>
            <td><input type="text" class="textbox" name="txtname[]" value="test1"/></td>
            <td><input type="text" class="textbox" name="txtfname[]"/></td>
            <td><input type="text" class="textbox" name="txtmname[]"/></td>
        </tr>
        <tr>
            <td><input type="text" class="textbox" name="txtname[]" value="test2"/></td>
            <td><input type="text" class="textbox" name="txtfname[]"/></td>
            <td><input type="text" class="textbox" name="txtmname[]"/></td>
        </tr>
    </table>
  </body>
</html>
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
<script>
    //Use an id on the table and get its rows
    let rows = $("#datesheet tr");

    //Now find the row you want
    let row_id = 1; // Not sure how you get this value.
    let current_row = rows[row_id];

    //Get the input you wants value
    let input_data = $(current_row).find("td input[name='txtname[]']").val()

    //Not sure what should be the same now.
    //If it is the other tds in this row then use selector above and set with val.
    //If is is other rows input with the same name... That would be a bit involved and I would need some time.

   //This is what other mean by unclear.


    alert(input_data);

</script>

标题
//在表上使用id并获取其行
设行=$(“#日期表tr”);
//现在找到你想要的那一行
让row_id=1;//不确定如何获得此值。
让当前行=行[行id];
//获取您想要的输入值
让input_data=$(当前_行).find(“td-input[name='txtname[]”)val()
//不知道现在应该是什么样子。
//如果是此行中的其他tds,则使用上面的选择器并设置val。
//如果是其他行输入相同的名称…这将是一个有点涉及,我需要一些时间。
//这就是“不清楚”的其他含义。
警报(输入_数据);

您的问题非常不清楚,请包含所有相关代码,并向我们展示您迄今为止的尝试。此外,您有多个元素具有相同的id,这不好,请使用类。我认为您需要将行号设置为数组的索引,不是吗?您不应该将数组指定给元素的id…id属性指定HTML元素的唯一id(该值在HTML文档中必须是唯一的)。您必须为每个HTML元素提供唯一的id。您能解释一下您试图做什么吗?您是否试图告诉远程服务器行的列已更改?我需要用id标识该文本框。它现在是id=“txtname[0]“。有没有办法把这个框设为$(“#textbox[0]”)。val();很明显,他说他想用id设置文本框的值。这是id='txtname'+i。”+“'您的文本框。您将如何使用jquery或js为其设置值假设您已经知道iWell的值要回答这个问题,我们确实需要在您的代码周围有更多的上下文…如果您有这样的行,您可以使用多维元素数组。
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>title</title>
  </head>
  <body>
    <table id="datesheet">
        <tr>
            <td><input type="text" class="textbox" name="txtname[]" value="test0"/></td>
            <td><input type="text" class="textbox" name="txtfname[]"/></td>
            <td><input type="text" class="textbox" name="txtmname[]"/></td>
        </tr>
        <tr>
            <td><input type="text" class="textbox" name="txtname[]" value="test1"/></td>
            <td><input type="text" class="textbox" name="txtfname[]"/></td>
            <td><input type="text" class="textbox" name="txtmname[]"/></td>
        </tr>
        <tr>
            <td><input type="text" class="textbox" name="txtname[]" value="test2"/></td>
            <td><input type="text" class="textbox" name="txtfname[]"/></td>
            <td><input type="text" class="textbox" name="txtmname[]"/></td>
        </tr>
    </table>
  </body>
</html>
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
<script>
    //Use an id on the table and get its rows
    let rows = $("#datesheet tr");

    //Now find the row you want
    let row_id = 1; // Not sure how you get this value.
    let current_row = rows[row_id];

    //Get the input you wants value
    let input_data = $(current_row).find("td input[name='txtname[]']").val()

    //Not sure what should be the same now.
    //If it is the other tds in this row then use selector above and set with val.
    //If is is other rows input with the same name... That would be a bit involved and I would need some time.

   //This is what other mean by unclear.


    alert(input_data);

</script>