嵌套循环内输入值未定义 - jquery
我已经尝试解决这个问题有一段时间了。我正在尝试获取嵌套循环内的表单输入的值。但它的到来是不确定的。知道为什么会这样吗?非常感谢您提前的帮助。这是示例代码。
我毫无问题地获取了 $(#fund-no-"+f).attr('value')
的值。它的 $("#year-" +year_amt + " -" + f).attr('value')
我无法检索。它是未定义的。如果我使用 $("#year-" +year_amt + "-" + f).val( )
相反,我根本没有得到任何价值。
var fund_datastring;
fund_datastring = "";
if(fundqty >0)
{
for(var f =1; f <= fundqty; f++)
{
fund_datastring += "&fund_no_" + f + "=" + $("#fund-no-" + f).attr('value') ;
var fundyear_datastring = "";
var hidden_fundid = $("#hidden-fund-id-"+f).attr('value');
var year_amt=2;
//for(var year_amt =2; year_amt < hidden_fundid; year_amt++)
while (year_amt < hidden_fundid)
{
fundyear_datastring += "&hidden_fund_"+f+ "=" + hidden_fundid + "&fund_"+ f +"_amount_"+ year_amt + "=" + $("#year-" + year_amt + "-" + f).attr('value') + "&fund_"+ f +"_year_"+ year_amt + "=" + year_amt;
year_amt++
}
hidden_fundid = "";
}
}
<div class="fsFieldHorizontal">
<input type="text" value="" class="fsField" size="32" name="year_1_1" id="year-1-1">
<input type="hidden" value="4" id="hidden-fund-id-1">
<a onclick="addFundForm(this); return false;" id="add-year-1" href="#">
<img alt="Add Year" src="images/add_16.png">
</a><br><br>
<div id="year-container-1">
<div id="year-2-1" style="margin: 5px 0px;">
<input type="text" value="" class="fsField" size="32" name="year_2_1" id="year-2-1">
<a onclick="removeFundYear("#year-2-1"); return false;" href="#">
<img alt="Remove Year 2" src="images/cancel_16.png">
</a><br>
<label for="year-2-1" class="fsSupporting">Year 2 Amount</label>
</div>
<div id="year-3-1" style="margin: 5px 0px;">
<input type="text" value="" class="fsField" size="32" name="year_3_1" id="year-3-1">
<a onclick="removeFundYear("#year-3-1"); return false;" href="#">
<img alt="Remove Year 3" src="images/cancel_16.png">
</a><br>
<label for="year-3-1" class="fsSupporting">Year 3 Amount</label>
</div>
</div>
</div>
I have been trying to solve this for a while. I am trying to get value of a form input which is inside a nested loop. But its coming undefined. Any idea why so? Thanks much for your help in advance. Here is the code for example.
I am getting the value of $(#fund-no-"+f).attr('value')
without any problem. Its $("#year-" + year_amt + "-" + f).attr('value')
that i cant retrieve. It comes undefined. If i use $("#year-" + year_amt + "-" + f).val()
instead i dont get any value at all.
var fund_datastring;
fund_datastring = "";
if(fundqty >0)
{
for(var f =1; f <= fundqty; f++)
{
fund_datastring += "&fund_no_" + f + "=" + $("#fund-no-" + f).attr('value') ;
var fundyear_datastring = "";
var hidden_fundid = $("#hidden-fund-id-"+f).attr('value');
var year_amt=2;
//for(var year_amt =2; year_amt < hidden_fundid; year_amt++)
while (year_amt < hidden_fundid)
{
fundyear_datastring += "&hidden_fund_"+f+ "=" + hidden_fundid + "&fund_"+ f +"_amount_"+ year_amt + "=" + $("#year-" + year_amt + "-" + f).attr('value') + "&fund_"+ f +"_year_"+ year_amt + "=" + year_amt;
year_amt++
}
hidden_fundid = "";
}
}
<div class="fsFieldHorizontal">
<input type="text" value="" class="fsField" size="32" name="year_1_1" id="year-1-1">
<input type="hidden" value="4" id="hidden-fund-id-1">
<a onclick="addFundForm(this); return false;" id="add-year-1" href="#">
<img alt="Add Year" src="images/add_16.png">
</a><br><br>
<div id="year-container-1">
<div id="year-2-1" style="margin: 5px 0px;">
<input type="text" value="" class="fsField" size="32" name="year_2_1" id="year-2-1">
<a onclick="removeFundYear("#year-2-1"); return false;" href="#">
<img alt="Remove Year 2" src="images/cancel_16.png">
</a><br>
<label for="year-2-1" class="fsSupporting">Year 2 Amount</label>
</div>
<div id="year-3-1" style="margin: 5px 0px;">
<input type="text" value="" class="fsField" size="32" name="year_3_1" id="year-3-1">
<a onclick="removeFundYear("#year-3-1"); return false;" href="#">
<img alt="Remove Year 3" src="images/cancel_16.png">
</a><br>
<label for="year-3-1" class="fsSupporting">Year 3 Amount</label>
</div>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新 2
正如我在下面怀疑的那样,您的 ID 不是唯一的:您有两个:
还有这个:
jQuery 可能正在找到第一个,它(当然)没有
值< /代码>。
更新 1
根据您的描述,最有可能的情况是您没有 ID 为
year-2-1
的元素(在第一次传递时)等等——或者您有多个这样的元素(这是无效的,因此不可预测)。因为从根本上来说,如果 DOM 正确,它就会起作用: http://jsbin.com/icume3 如果没有具有该 ID 的元素,您将得到您所描述的内容: http://jsbin.com/icume3/2旧的、显然不正确的答案 (以多种方式):
您还没有显示您的标记,但我的猜测是
.val()
在.attr("value")
的位置工作不是因为您正在处理没有value
属性的表单元素,像(见下文) 。select
.val
获取元素的值,根据类型不同而不同它是元素的。编辑:显然 jQuery 处理
attr
中的“值”,至少对于select
来说,至少在我尝试过的浏览器上(包括 IE6,所以): http://jsbin.com/oxega3 所以不是这样。Update 2
As I suspected below, your IDs were not unique: You have both this:
And this:
jQuery is probably finding the first one, which does not (of course) have a
value
.Update 1
From what you're describing, the most likely scenario is that you don't have an element with the ID
year-2-1
(on the first pass) and so on — or you have more than one such element (which is invalid and so unpredictable). Because fundamentally, if the DOM is right, it works: http://jsbin.com/icume3 And if there's no element with that ID, you get exactly what you're describing: http://jsbin.com/icume3/2Old, apparently incorrect answer (in more ways than one):
You haven't shown your markup, but my guess would be that
.val()
is working where.attr("value")
is not because you're dealing with a form element that doesn't have avalue
attribute,like a(see below).select
.val
gets the value of the element, which varies depending on what kind of element it is.Edit: Apparently jQuery handles "value" in
attr
, at least forselect
s, at least on the browsers I tried (including IE6, so): http://jsbin.com/oxega3 So not that.