2012-03-29 8 views
0

私はこのような形式を持つ:最後の値を長さ+ 1でカウンターする方法は?

<div id='add_field' class='locbutton'><a href='#' title='Add'>Add</a></div> 
<div id='remove_field' class='locbutton2'><a href='#' title='Delete'>Delete</a></div> 
<div id='idisikpi'> 
<div id='idheadisi1' class='headisi'> 
<div class='performancedetail'><input type='text' name='idperformancedetail[1][]' 
value='2' id='idperformancedetail[1][]'></div> 
<div class='performancedetail'><input type='text' name='idperformancedetail[2][]' 
value='9' id='idperformancedetail[2][]'></div> 
</div> 
</div> 

これはjqueryのスクリプトです:

function CreateInput() 
{ 

    var $headisi = $("<div>"), 
    $idperformancedetail=$("<div>"); 

    var headisi_count = $("div.headisi").length+1; 
    var idperformancedetail_count = 
    $("[id^='idperformancedetail']").last().val().length+1; 

    $headisi.addClass("headisi"); 
    $idperformancedetail.addClass("performancedetail"); 

    var $input_idperformancedetail = $("<input>") 

    $input_idperformancedetail 
    .attr("name", "idperformancedetail["+headisi_count+"][]") 
    .attr("type", "text") 
    .attr("id", "idperformancedetail["+headisi_count+"][]") 
    .attr("value", idperformancedetail_count); 
    $idperformancedetail.append($input_idperformancedetail); 

    $headisi.append($idperformancedetail); 

    return $headisi; 
} 



$("#add_field a").click(function(e) 
{ 
$("#idisikpi").append(CreateInput()); 

e.preventDefault(); 
}); 

$("#remove_field a").click(function(e){ 
$("#idisikpi div.headisi:last-child").remove(); 
CalculateTotal(); 

e.preventDefault(); 
}); 

私は次の入力がidperformancedetailの最後の値からカウンタであり、私は10のように、次の入力に値を旺旺、11,12、...次の入力でカウンタ値を取得するにはどうすればよいですか?私を助けてください:(

答えて

0

をあなたは整数(parseInt)として$("[id^='idperformancedetail']").last().val()を解析する必要があり、そうでなければ、よ。 。また、あなたは.lengthを使用しない連結した文字列を取得しますが、val()

var idperformancedetail_count = parseInt($("[id^='idperformancedetail']").last().val()) + 1; 

JSFiddle demonstrationval()は、divvalue属性を自分で設定した後にのみ機能します。これであなたのinputの値は完全に無視されます。

+0

その解決策。あなたは:) –

0

はそうのような既存のチェックボックスの数を取得します:

var existingInputCount = $('input[name^=idperformancedetail]').length; 

それに応じてexistingInputCountを使用

関連する問題