gpt4 book ai didi

javascript - 从文本框数组中选择一个文本框

转载 作者:行者123 更新时间:2023-12-02 17:58:20 24 4
gpt4 key购买 nike

我想从文本框数组中选择一个文本框。为此,我尝试了以下方法,但我没有得到任何输出

HTML 代码

<input type="text" id="total[]"/>
<input type="text" id="total[]"/>
<input type="text" id="total[]"/>

jquery

$i=0;
for($i = 0;$i < total.length;$i++;)
{
$("#total\\[$i\\]").val($i);
}

上面的代码没有显示任何内容。

请帮我解决问题

编辑

我已经更新了我的演示,有

textbox at pos(0,2) total of(0,0) and (0,1) should come
textbox at pos(1,2) total of(1,0) and (1,1) should come
and
textbox at pos(2,0) total of(0,0) and (1,0) should come
textbox at pos(2,1) total of(0,1) and (1,1) should come

Demo

最佳答案

要将输入应用于数组以进行服务器端处理,您必须使用name 属性。 id 属性在文档范围内必须是唯一的。如果您有此标记:

<input type="text" name="total[]"/>
<input type="text" name="total[]"/>
<input type="text" name="total[]"/>

您可以通过将函数传递给 val() 方法来实现您想要的目的:

$('input[name^=total]').val(function(i){
return i;
});

JSFiddle

关于javascript - 从文本框数组中选择一个文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20800585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com