Javascript 从DOM获取.append值
在下面的函数中,我找到一个ID的最大计数,然后加1。除了添加到DOM中的新值看不到外,所有操作都正常,因此在第二次调用该函数时它们不会增加 我是否没有正确地向DOM添加新值Javascript 从DOM获取.append值,javascript,jquery,Javascript,Jquery,在下面的函数中,我找到一个ID的最大计数,然后加1。除了添加到DOM中的新值看不到外,所有操作都正常,因此在第二次调用该函数时它们不会增加 我是否没有正确地向DOM添加新值 function addEdu(){ var currentCount = $('input.uniqueId').val(); currentCount = Math.max(currentCount); var nextCount = parseInt(currentCount) + 1;
function addEdu(){
var currentCount = $('input.uniqueId').val();
currentCount = Math.max(currentCount);
var nextCount = parseInt(currentCount) + 1;
var newEdu = "<input type='hidden' name='fieldId[]' value='" + nextCount + "' class='uniqueId' /><p class='dual'><input type='text' name='educationTitle[]' "; //Shortened for clarity
$("#eduHistory").append(newEdu);
$( ".datepicker" ).datepicker();
}
函数addEdu(){
var currentCount=$('input.uniqueId').val();
currentCount=数学最大值(currentCount);
var nextCount=parseInt(currentCount)+1;
var newEdu=“$('input.uniqueId').val()
将使用classuniqueId
为您提供第一个输入的值。如果您只希望有一个这样的输入,只需更改输入的值。否则您可能必须选择最后一个输入$('input.uniqueId:last').val()
此外,Math.max需要2个参数,它的用途是什么?$('input.uniqueId').val()
将使用类uniqueId
为您提供第一个输入的值。如果您只希望有一个这样的输入,只需更改输入的值。否则,您可能必须选择最后一个输入$('input.uniqueId:last').val()
另外,Math.max有两个参数,它的用途是什么?您试图确定下一个。uniqueId
的方法是错误的。
首先,$(“input.uniqueId”).val()
将为您提供选择中第一个元素的值。
第二个问题是Math.max(currentCount)
的问题,它总是返回currentCount
的值,正如前面提到的,这是第一个元素的值
这应按预期工作:
// get an array with all uniqueIds
var currentIds = $("input.uniqueId").map(function() {
return parseInt($(this).val(), 10);
});
// determine the biggest uniqueId
var currentCount = Math.max.apply(Math, currentIds);
// next uniqueId (if there is no "input.uniqueId" then currentCount will be -Infinity)
var nextCount = (currentCount === -Infinity ? 0 : currentCount) + 1;
// ...
您试图确定下一个.uniqueId
的方式是错误的。
首先,$(“input.uniqueId”).val()
将为您提供选择中第一个元素的值。
第二个问题是Math.max(currentCount)
的问题,它总是返回currentCount
的值,正如前面提到的,这是第一个元素的值
这应按预期工作:
// get an array with all uniqueIds
var currentIds = $("input.uniqueId").map(function() {
return parseInt($(this).val(), 10);
});
// determine the biggest uniqueId
var currentCount = Math.max.apply(Math, currentIds);
// next uniqueId (if there is no "input.uniqueId" then currentCount will be -Infinity)
var nextCount = (currentCount === -Infinity ? 0 : currentCount) + 1;
// ...
似乎是正确的…您是否在$(document).ready(function(){//code here}
块中调用此函数?var currentCount=$('input.uniqueId').val();您应该在.each()操作中执行此操作,因为您没有明确说明要获取的值。或者您可能需要.size()而不是.val()?@rnirnber-你的意思肯定是length
,而不是size()
?@rnirnber-我的观点正是->“首选.length属性”?似乎正确…你是在$(文档)中调用这个函数吗操作,因为您没有明确说明要获取的值。或者您可能想要.size()而不是.val()?@rnirnber-您的意思肯定是length
,而不是size()
?@rnirnber-我的重点是->“首选.length属性”?我将为$('input.uniqueId').val()提供未知数量的值
。我试图在创建时将uniqueId
的值增加1。Math.max
是我试图获得$('input.uniqueId').val()的最大值的尝试。
@unsertivevar id=$(/input.uniqueId”).map(函数(){return parseInt($(this.val(),10);});var currentCount=Math.max.apply(Math,ids);
@Andreas-谢谢!刚刚尝试过,看起来效果很好。如果您将此作为答案添加,我将接受。@Musa-谢谢您的帮助!我将为$('input.uniqueId').val()提供未知数量的值
。我试图在创建时将uniqueId
的值增加1。Math.max
是我试图获得$('input.uniqueId').val()的最大值的尝试。
@unsertivevar id=$(/input.uniqueId”).map(函数(){return parseInt($(this.val(),10);});var currentCount=Math.max.apply(Math,ids);
@Andreas-谢谢!刚刚试过,看起来效果很好。如果你加上这个作为答案,我会接受。@Musa-谢谢你的帮助!