2017-08-21 1 views
0

Ich erstelle eine Reihe von Textfeldern und einige Dropdowns mit jQuery. Die Add-Funktion arbeitet ohne Probleme aus.Zurücksetzen der Fehlernummer beim Löschen aus der Liste

Mein Problem ist mit der Löschfunktion Funktion funktioniert gut, solange der Benutzer nacheinander löscht, aber wenn der Benutzer aus einigen wo sonst die Zahlenfolge vermasselt löschen. Das bricht die 1,2,3,4 ... usw. und setzt die Nummer, die zuletzt gelöscht wurde.

Wenn der Benutzer z. B. Nummer 4 von 7 Fehlern löscht, stellt die Funktion die letzte Zahl auf 4, wenn der Benutzer auf die Schaltfläche Hinzufügen klickt. Die nächste generierte Nummer ist 5 nicht die korrekte letzte Nummer. Ich möchte den Rest der Zahlen ausruhen, wenn etwas aus der Mitte entfernt wird.

Ich speichere die letzte Nummer in einem versteckten Feld namens stValue, das beim Löschen zurückgesetzt wird.

Mein Problem ist hier, ich kann nicht meinen Kopf herum zu denken, Weg, um dies beim Löschen von etwas wo sonst zurückgesetzt und dann setzen Sie die gesamte Fehlernummer Zeilennummern, wenn etwas aus der Mitte entfernt werden. Kannst du mir helfen das unten ist mein Code.

jsFiddle helfen besser

JQuery zu verstehen:

//Add and remove function for the error text boxes 
$(document).ready(function() { 
    $(document).on('click','.addRow',function() { 
     var div = $("<div />"), 
      btnId = $(this).data("bid").match(/\d+/);//Breaks the number from the ID using .match 

     div.html(copy()); //Creates a new div container 
     $('.error-Column').append(div); //Insert all the HTML into the new div 

     $('#addRow_'+btnId).prop("disabled", true);//Disables the add button once clicked. 
    }); 

    //Remove the text filed from the list and resets the error number 
    $(document).on('click', '.delRow', function() { 
     var //btnId = $(this).data("bid").match(/\d+/),//Breaks the number from the ID using .match 
      maxNoList = $('input[class="addRow"]').length, 
      errNoList = maxNoList - 1; 


     alert(errNoList); 


     //btnId = btnId - 1; //Calculates the ID number of the previous button 

     $('#addRow_'+errNoList).prop('disabled',false);// Enables the previous add button 

     $('#stValue').val(errNoList); //Set the value of stValue when removing the text filed 
            //So the error numbers will be generated accordingly when Add is clicked again. 

     $(this).parent().remove(); //Remove the text row from the list. 
    }); 
}); 

//HTML function which will be called by the button click event for the add button 
function copy() { 
    var stNum = document.getElementById("stValue"), 
     genNum = (document.getElementById("stValue").value - 1)+2; 

    stNum.value = genNum; 

    // language=HTML 
    return '<input class="errorCount" size="1" value="'+genNum+'" style="margin-left: 2%" readonly/>\n' + 
     '<select class="errorName" style="margin-left: 6%">\n' + 
     '<option selected disabled>----- Select Error -----</option>\n' + 
     '</select>\n' + 
     '<input type="button" class="addRow" id="addRow_'+genNum+'" data-bid="addRow_'+genNum+'" value="Add" />\n' + 
     '<input type="button" class="delRow" id="delRow_'+genNum+'" data-bid="delRow_'+genNum+'" value="Delete"/><br />' 
} 

HTML:

<div id="jType-container"> 
    <div id="error-Add-Container"> 
<div id="error-Column-Headings"> 
    Error Number<span style="margin-left: 8%">Error Name</span> 
</div> 
<div class="error-Column"> 
<input class="errorCount" size="1" value="1" style="margin-left: 2%"/> 
    <input type="hidden" value="1" id="stValue"/> 
    <select class="errorName" style="margin-left: 6%"> 
     <option selected disabled>----- Select Error -----</option> 
    </select> 
    <input type="button" data-bid="addRow_1" id="addRow_1" class="addRow" value="Add"/> 
    </div> 
</div> 
</div> 

** UPDATE: ** völlig verändert den Code jetzt ist es viel einfacher, das Hinzufügen der gelöst antworte hier, damit es jemandem in der Zukunft helfen kann.

//Add and remove function for the error text boxes 
 
$(document).ready(function() { 
 
    $(document).on('click', '.addRow', function() { 
 
    var div = $("<div />"), 
 
     btnId = $(this).data("bid").match(/\d+/); //Breaks the number from the ID using .match 
 

 
    div.html(copy()); //Creates a new div container 
 
    $('.error-Column').append(div); //Insert all the HTML into the new div 
 

 
    $('#addRow_' + btnId).prop("disabled", true); //Disables the add button once clicked. 
 
    }); 
 

 
    //Remove the text filed from the list and resets the error number 
 
    $(document).on('click', '.delRow', function() { 
 
    var btnId = $("#stValue").val(); //Read the value of stValue 
 

 
    btnId = btnId - 1; //Deduct 1 from the value to get the last ID 
 

 
    //Enables the 1st add button if the value equals 1 
 
    if (btnId === 1) { 
 
     $('#addRow_' + btnId).prop('disabled', false); 
 
    } 
 

 
    $(this).parent().remove(); //Remove the text row from the list. 
 
    resetErrorNo(); //Calls the reset function 
 
    }); 
 
}); 
 

 
//Reset the entire error count number index 
 
function resetErrorNo() { 
 
    $(".errorCount").each(function(index, _this) { 
 
    $(this).val(index + 1); 
 
    $("#stValue").val(index + 1); 
 
    }); 
 
} 
 

 
//HTML function which will be called by the button click event for the add button 
 
function copy() { 
 
    var stNum = document.getElementById("stValue"), 
 
    genNum = (document.getElementById("stValue").value - 1) + 2; 
 

 
    stNum.value = genNum; 
 

 
    // language=HTML 
 
    return '<input class="errorCount" size="1" value="' + genNum + '" style="margin-left: 2%" readonly/>\n' + 
 
    '<select class="errorName" style="margin-left: 6%">\n' + 
 
    '<option selected disabled>----- Select Error -----</option>\n' + 
 
    '</select>\n' + 
 
    '<input type="button" class="addRow" id="addRow_' + genNum + '" data-bid="addRow_' + genNum + '" value="Add" />\n' + 
 
    '<input type="button" class="delRow" id="delRow_' + genNum + '" data-bid="delRow_' + genNum + '" value="Delete"/><br />' 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="jType-container"> 
 
    <div id="error-Add-Container"> 
 
    <div id="error-Column-Headings"> 
 
     Error Number<span style="margin-left: 8%">Error Name</span> 
 
    </div> 
 
    <div class="error-Column"> 
 
     <input class="errorCount" size="1" value="1" style="margin-left: 2%" /> 
 
     <input type="hidden" value="1" id="stValue" /> 
 
     <select class="errorName" style="margin-left: 6%"> 
 
          <option selected disabled>----- Select Error -----</option> 
 
         </select> 
 
     <input type="button" data-bid="addRow_1" id="addRow_1" class="addRow" value="Add" /> 
 
    </div> 
 
    </div> 
 
</div>

+0

so schwer zu verstehen, was u versuchen zu tun .. u kann eine jsfiddle liefern und uns erklären, was bedeutet 'die Zahlenfolge durcheinander bekommen 'gemein? –

+0

@ oliv37 jsFiddle hinzugefügt. – Sand

Antwort

3

Versuchen Sie es mit, wenn Sie eine Zeile löschen, alle Eingaben mit neuen Nummer aktualisieren.

$(".errorCount").each(function(index, _this) { 
    $(this).val(index + 1); 
}); 

Voll-Code

//Add and remove function for the error text boxes 
 
$(document).ready(function() { 
 
    $(document).on('click', '.addRow', function() { 
 
    var div = $("<div />"), 
 
     btnId = $(this).data("bid").match(/\d+/); //Breaks the number from the ID using .match 
 

 
    div.html(copy()); //Creates a new div container 
 
    $('.error-Column').append(div); //Insert all the HTML into the new div 
 

 
    $('#addRow_' + btnId).prop("disabled", true); //Disables the add button once clicked. 
 
    }); 
 

 
    //Remove the text filed from the list and resets the error number 
 
    $(document).on('click', '.delRow', function() { 
 
    var //btnId = $(this).data("bid").match(/\d+/),//Breaks the number from the ID using .match 
 
     maxNoList = $('input[class="addRow"]').length, 
 
     errNoList = maxNoList - 1; 
 

 

 

 
    //btnId = btnId - 1; //Calculates the ID number of the previous button 
 

 
    $('#addRow_' + errNoList).prop('disabled', false); // Enables the previous add button 
 

 
    $('#stValue').val(errNoList); //Set the value of stValue when removing the text filed 
 
    //So the error numbers will be generated accordingly when Add is clicked again. 
 

 
    $(this).parent().remove(); //Remove the text row from the list. 
 
    rearrange(); 
 
    }); 
 
}); 
 

 
function rearrange() { 
 

 
    $(".errorCount").each(function(index, _this) { 
 
    $(this).val(index + 1); 
 
    }); 
 

 
} 
 

 
//HTML function which will be called by the button click event for the add button 
 
function copy() { 
 
    var stNum = document.getElementById("stValue"), 
 
    genNum = (document.getElementById("stValue").value - 1) + 2; 
 

 
    stNum.value = genNum; 
 

 
    // language=HTML 
 
    return '<input class="errorCount" size="1" value="' + genNum + '" style="margin-left: 2%" readonly/>\n' + 
 
    '<select class="errorName" style="margin-left: 6%">\n' + 
 
    '<option selected disabled>----- Select Error -----</option>\n' + 
 
    '</select>\n' + 
 
    '<input type="button" class="addRow" id="addRow_' + genNum + '" data-bid="addRow_' + genNum + '" value="Add" />\n' + 
 
    '<input type="button" class="delRow" id="delRow_' + genNum + '" data-bid="delRow_' + genNum + '" value="Delete"/><br />' 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<div id="jType-container"> 
 
    <div id="error-Add-Container"> 
 
    <div id="error-Column-Headings"> 
 
     Error Number<span style="margin-left: 8%">Error Name</span> 
 
    </div> 
 
    <div class="error-Column"> 
 
     <input class="errorCount" size="1" value="1" style="margin-left: 2%" /> 
 
     <input type="hidden" value="1" id="stValue" /> 
 
     <select class="errorName" style="margin-left: 6%"> 
 
     <option selected disabled>----- Select Error -----</option> 
 
     </select> 
 
     <input type="button" data-bid="addRow_1" id="addRow_1" class="addRow" value="Add" /> 
 
    </div> 
 
    </div> 
 
</div>

Verwandte Themen