2016-09-21 1 views
0

私はスクリプトを実行して選択リストのオプションに数字を表示します。ユーザーが特定の値をチェックすると、彼は彼の請求書を支払うことができる時間を示す数値を表示します。X番号の選択からの保護

(注)このコード:

var tabelaParcelas = [2,3,4,6,8,10,12]; 

$(document).ready(function(){ 
    update(); 
}); 

$('input[type=checkbox]').click(function(){ 
    update(); 
}) 

function update(){ 
    var list = $('#instNum2'); // use selector only once whenever possible for better performance 

    // clear any existing options from the dropdown list 
    list.html("") 

    // count checked boxes 
    var checked_boxes = 0 
    $(".check_list").each(function(){ 
    if(this.checked){ 
     checked_boxes++; 
    } 
    }); 

    // append options to the select list 
    for(var i=0;i<checked_boxes;i++){ 
    var option = $("<option>", {value: tabelaParcelas[i], "text": tabelaParcelas[i]}); 
     list.append(option); 
    } 


} 

正しいです!しかし、事は、私は1xを表示する必要があります、そしてそれは全く表示されていないです、1xは、あなたが選択したどれくらいのボックスであっても常に表示されるべきです、そして、空のオプション、人々はあなたのコードを実行すると、あなたは私がするつもりはないと思うwant.Butかを理解しようとすることができるように、それだけであまりにも新たな...

+0

ショーのHTMLを追加することなく、12xを表示しておく必要がありながら、答えを与える。私はちょうど –

答えて

0
var tabelaParcelas = [2,3,4,6,8,10,12]; 

$(document).ready(function(){ 
    update(); 
}); 

$('input[type=checkbox]').click(function(){ 
    update(); 
}) 

function update(){ 
    var list = $('#instNum2'); // use selector only once whenever possible for better performance 

    // clear any existing options from the dropdown list 
    list.html("") 

    // count checked boxes 
    var checked_boxes = 0 
    $(".check_list").each(function(){ 
    if(this.checked){ 
     checked_boxes++; 
    } 
    }); 

    //add limit to check_boxes 
    //This section was not in original code 
    // Could also do if(checked_boxes > tabelaParcelas.length) {checked_boxes = tabelaParcelas.length;} 
    //Would make it more dynamic. 
    //This is added to limit the number of options added to the length of the array at the top. 
    //This will prevent blank options from being added as requested in the question. 
    if(checked_boxes > 6) { 
     checked_boxes = 6; 
    } 

    //add 1x option to empty list 
    //This was not in original code 
    //This was added as the list was cleared when this function is run and they always wanted 1x to appear as an option. 
    list.append("<option value='1' selected>1</option>"); 

    // append options to the select list 
    for(var i=0;i<checked_boxes;i++){ 
    var option = $("<option>", {value: tabelaParcelas[i], "text": tabelaParcelas[i]}); 
     list.append(option); 
    } 


} 
+0

答えていただきありがとうございますが、check_boxesの制限のコードは、6以上の値を選択した後に値が表示されません...任意のアイデア? –

+0

私は誤字があるように見えます。私の追加制限では、私はcheck_boxesではなくchecked_boxesを持っていました。今編集されています。 – Chris

+2

あなたの答えを説明してください。元のコードに何が間違っていたのか、それをどのように修正したのかを教えてください。 – Barmar

関連する問題