2016-04-22 3 views
0

dynamisch id und weisen dort

$(document).ready(function() { 
 
    $("body").tooltip({ 
 
    selector: '[data-toggle=tooltip]' 
 
    }); 
 
}); 
 

 
var unique_id=0; 
 
$('#update').on('click', function() { 
 
    if ($('#myText').val().length == 0) { 
 
    alert('Are you Kidding ? ;)') 
 
    return 
 
    } 
 
    unique_id++; 
 
    $('#result').append('<li>' + '<input id="checkboxFourInput" type="checkbox" class="todo-item" /> <label for="checkboxFourInput"></label>' + $('#myText').val() + '<i class="glyphicon glyphicon-remove todo-item-del"></i>' + '</li>'); 
 
    // $('#myText').val("").attr("disabled", "disabled"); 
 

 
    // update value 
 
    $('#myText').val('').change(); 
 
}); 
 

 
$('#myText').on("focus click", function() { 
 
    $('#update').removeClass("gray_text"); 
 
    $('#update').addClass("show"); 
 
}); 
 
$('#edit').on('mouseup', function() { 
 
    console.log($('#myText')); 
 
    $('#myText').prop("disabled", false).focus().select(); 
 
}); 
 

 
$('#myText').on("focus click", function() { 
 
    $('#update').removeClass("gray_text"); 
 
    $('#update').addClass("show"); 
 
}) 
 

 
$('.hide').on("click", function() { 
 
    $('.hide').removeClass("show"); 
 
}); 
 

 
$('#result ').on('click', 'li .glyphicon-remove', function() { 
 
    // alert('Clicked'); 
 
    $(this).closest('li').remove(); 
 
});
.editable_text{ 
 
    margin: 20px; 
 
    /*padding: 15px;*/ 
 
    border: 1px solid #ccc; 
 
    width: 300px; 
 
    border-radius: 5px; 
 
    background: white; 
 
    display: inline-block; 
 
    text-align: left; 
 
} 
 
ul#result{ 
 
    padding: 0; 
 
    margin: 0; 
 
    position: relative; 
 
    top: -40px; 
 
    border-top: 1px solid #ccc; 
 
} 
 
#result li { 
 
    padding: 15px 25px; 
 
    border-bottom: 1px solid #ccc; 
 
    list-style: none; 
 
    font-size: 18px; 
 
    font-weight: 100; 
 
    color: #555; 
 
    letter-spacing: 1px; 
 
    width: 100%; 
 
} 
 
#myText{ 
 
    margin: 0; 
 
    padding: 20px 30px 20px 40px; 
 
    border: transparent; 
 
    border-bottom: 1px solid #ccc; 
 
    position: relative; 
 
    /*left: -16px;*/ 
 
    width: 100%; 
 
    top: -40px; 
 
} 
 

 
.btns{ 
 
    width:100%; 
 
    position: relative; 
 
    padding: 20px 0; 
 
} 
 
.plus { 
 
    padding: 0 20px; 
 
    cursor: pointer; 
 
    position: relative; 
 
    right: 2px; 
 
    z-index: 1; 
 
} 
 
.okay{ 
 
    float: right; 
 
    position: relative; 
 
    right: -2px; 
 
    top: 0px; 
 
    padding: 0 20px; 
 
    cursor: pointer; 
 
    z-index: 1; 
 
} 
 
.gray_text{ 
 
    opacity: 0.2; 
 
    color: #222; 
 
} 
 
.show{ 
 
    opacity: 1; 
 
    color: blue; 
 
} 
 
input[type=checkbox] { 
 
    visibility: hidden; 
 
} 
 
#result label { 
 
    cursor: pointer; 
 
    position: absolute; 
 
    width: 24px; 
 
    height: 24px; 
 
    top: 15px; 
 
    left: 15px; 
 
    background: #fff; 
 
    border: 1px solid #4AADF1; 
 
    border-radius: 100%; 
 
} 
 
#result label:focus{ 
 
    background: #4AADF1; 
 
} 
 
#result label:after { 
 
    opacity: 0.2; 
 
    content: ''; 
 
    position: absolute; 
 
    width: 12px; 
 
    height: 5px; 
 
    left: 5px; 
 
    top: 7px; 
 
    background: transparent; 
 
    border: 2px solid white; 
 
    border-top: none; 
 
    border-right: none; 
 
    -webkit-transform: rotate(-45deg); 
 
    -moz-transform: rotate(-45deg); 
 
    -o-transform: rotate(-45deg); 
 
    -ms-transform: rotate(-45deg); 
 
    transform: rotate(-45deg); 
 
} 
 

 
/** 
 
* Create the hover event of the tick 
 
*/ 
 
#result label:hover::after { 
 
    opacity: 0.5; 
 
} 
 

 
/** 
 
* Create the checkbox state for the tick 
 
*/ 
 
#result input[type=checkbox]:checked + label:after { 
 
    opacity: 1; 
 
} 
 
#result input[type=checkbox]:checked + label{ 
 
    background: #4AADF1; 
 
} 
 
.todo-item-del{ 
 
    text-align: right; 
 
    float: right; 
 
}
<!-- Latest compiled and minified CSS --> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 
<!-- Google JS --> 
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
 
<!-- Latest compiled and minified JavaScript --> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 

 
<div class="box"> 
 
    <div class="editable_text"> 
 
    <div class="btns"> 
 
     <a id="edit" class="pull-left plus" data-toggle="tooltip" data-placement="bottom" title="Click to Add"> 
 
     <i class="glyphicon glyphicon-plus"></i> 
 
     </a> 
 

 
     <a id="update" class="gray_text okay" data-toggle="tooltip" data-placement="right" title="Click to Okay"> 
 
     <i class="glyphicon glyphicon-ok"></i> 
 
     </a> 
 
    </div> 
 
    <input type='text' id="myText" placeholder="Add new goals"/> 
 
    <ul id="result"></ul> 
 
    </div> 
 
</div>

Hallo beschriften,

Ich versuche, eine To-Do-Liste für mein Admin-Dashboard zu implementieren, wo ich überboten up, in diesem ich war ein li in meiner To-Do-Liste als ein Element an, zum allerersten Mal ist es gut funktioniert, bekomme checkbox, die auch funktioniert, aber wenn ich die zweite To-Do, wird es immer Fehler und das Kontrollkästchen funktioniert nicht.

hier ist der Code, den ich habe versucht, aber vermöge folgenden wird die Geige link

Antwort

1

2 Ausgaben

$('#result').append('<li>' + '<input "$(this).attr("id",$(this).attr("id")+"_"+(unique_id))" type="checkbox" class="todo-item" /> <label for="(unique_id)"></label>' + $('#myText').val() + '<i class="glyphicon glyphicon-remove todo-item-del"></i>' + '</li>');

gelang erhalten:

  • id für das Kontrollkästchen werden muss, basierend auf unique_id
  • #result label { ... top: 15px; ... } bewirkt, dass jedes Kontrollkästchen an derselben y-Position fixiert wird.

$(document).ready(function() { 
 
    $("body").tooltip({ 
 
    selector: '[data-toggle=tooltip]' 
 
    }); 
 
}); 
 

 
var unique_id = 0; 
 
$('#update').on('click', function() { 
 
    if ($('#myText').val().length == 0) { 
 
    alert('Are you Kidding ? ;)') 
 
    return 
 
    } 
 
    unique_id++; 
 
    $('#result').append('<li>' + '<input id="checkbox'+unique_id+'Input" type="checkbox" class="todo-item" /> <label for="checkbox'+unique_id+'Input"></label>' + $('#myText').val() + '<i class="glyphicon glyphicon-remove todo-item-del"></i>' + '</li>'); 
 
    // $('#myText').val("").attr("disabled", "disabled"); 
 

 
    // update value 
 
    $('#myText').val('').change(); 
 
}); 
 

 
$('#myText').on("focus click", function() { 
 
    $('#update').removeClass("gray_text"); 
 
    $('#update').addClass("show"); 
 
}); 
 
$('#edit').on('mouseup', function() { 
 
    console.log($('#myText')); 
 
    $('#myText').prop("disabled", false).focus().select(); 
 
}); 
 

 
$('#myText').on("focus click", function() { 
 
    $('#update').removeClass("gray_text"); 
 
    $('#update').addClass("show"); 
 
}) 
 

 
$('.hide').on("click", function() { 
 
    $('.hide').removeClass("show"); 
 
}); 
 

 
$('#result ').on('click', 'li .glyphicon-remove', function() { 
 
    // alert('Clicked'); 
 
    $(this).closest('li').remove(); 
 
});
.editable_text { 
 
    margin: 20px; 
 
    /*padding: 15px;*/ 
 
    border: 1px solid #ccc; 
 
    width: 300px; 
 
    border-radius: 5px; 
 
    background: white; 
 
    display: inline-block; 
 
    text-align: left; 
 
} 
 
ul#result { 
 
    padding: 0; 
 
    margin: 0; 
 
    position: relative; 
 
    top: -40px; 
 
    border-top: 1px solid #ccc; 
 
} 
 
#result li { 
 
    padding: 15px 25px; 
 
    border-bottom: 1px solid #ccc; 
 
    list-style: none; 
 
    font-size: 18px; 
 
    font-weight: 100; 
 
    color: #555; 
 
    letter-spacing: 1px; 
 
    width: 100%; 
 
} 
 
#myText { 
 
    margin: 0; 
 
    padding: 20px 30px 20px 40px; 
 
    border: transparent; 
 
    border-bottom: 1px solid #ccc; 
 
    position: relative; 
 
    /*left: -16px;*/ 
 
    width: 100%; 
 
    top: -40px; 
 
} 
 
.btns { 
 
    width: 100%; 
 
    position: relative; 
 
    padding: 20px 0; 
 
} 
 
.plus { 
 
    padding: 0 20px; 
 
    cursor: pointer; 
 
    position: relative; 
 
    right: 2px; 
 
    z-index: 1; 
 
} 
 
.okay { 
 
    float: right; 
 
    position: relative; 
 
    right: -2px; 
 
    top: 0px; 
 
    padding: 0 20px; 
 
    cursor: pointer; 
 
    z-index: 1; 
 
} 
 
.gray_text { 
 
    opacity: 0.2; 
 
    color: #222; 
 
} 
 
.show { 
 
    opacity: 1; 
 
    color: blue; 
 
} 
 
input[type=checkbox] { 
 
    visibility: hidden; 
 
} 
 
#result label { 
 
    cursor: pointer; 
 
    position: absolute; 
 
    width: 24px; 
 
    height: 24px; 
 
    /*top: 15px;*/ 
 
    left: 15px; 
 
    background: #fff; 
 
    border: 1px solid #4AADF1; 
 
    border-radius: 100%; 
 
} 
 
#result label:focus { 
 
    background: #4AADF1; 
 
} 
 
#result label:after { 
 
    opacity: 0.2; 
 
    content: ''; 
 
    position: absolute; 
 
    width: 12px; 
 
    height: 5px; 
 
    left: 5px; 
 
    top: 7px; 
 
    background: transparent; 
 
    border: 2px solid white; 
 
    border-top: none; 
 
    border-right: none; 
 
    -webkit-transform: rotate(-45deg); 
 
    -moz-transform: rotate(-45deg); 
 
    -o-transform: rotate(-45deg); 
 
    -ms-transform: rotate(-45deg); 
 
    transform: rotate(-45deg); 
 
} 
 
/** 
 
* Create the hover event of the tick 
 
*/ 
 

 
#result label:hover::after { 
 
    opacity: 0.5; 
 
} 
 
/** 
 
* Create the checkbox state for the tick 
 
*/ 
 

 
#result input[type=checkbox]:checked + label:after { 
 
    opacity: 1; 
 
} 
 
#result input[type=checkbox]:checked + label { 
 
    background: #4AADF1; 
 
} 
 
.todo-item-del { 
 
    text-align: right; 
 
    float: right; 
 
}
<!-- Latest compiled and minified CSS --> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 
<!-- Google JS --> 
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
 
<!-- Latest compiled and minified JavaScript --> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 

 
<div class="box"> 
 
    <div class="editable_text"> 
 
    <div class="btns"> 
 
     <a id="edit" class="pull-left plus" data-toggle="tooltip" data-placement="bottom" title="Click to Add"> 
 
     <i class="glyphicon glyphicon-plus"></i> 
 
     </a> 
 

 
     <a id="update" class="gray_text okay" data-toggle="tooltip" data-placement="right" title="Click to Okay"> 
 
     <i class="glyphicon glyphicon-ok"></i> 
 
     </a> 
 
    </div> 
 
    <input type='text' id="myText" placeholder="Add new goals" /> 
 
    <ul id="result"></ul> 
 
    </div> 
 
</div>

+0

Danke @Fabricator –

1

Eine einfache Möglichkeit, eine eindeutige ID in JS ist mit dem Date-Objekt und getTime(), die die Anzahl der Millisekunden seit 1. Januar 1970 00.00.00 UTC kehrt zu erstellen .

new Date().getTime(); // 1461348496454 

Es scheint jedoch, dass dies nicht der Grund ist, warum die Label/Checkboxen zu fehlen scheinen. Sie sind dort; Sie sind alle an der gleichen Stelle, weil sie position: absolute sind. Sie müssen nur position: relative zu Ihrem li hinzufügen.

Eine Randnotiz, ich empfehle eine Art Client-seitiges Templating für diese Art von Aufgabe zu verwenden. Es wird es einfacher machen, Probleme zu erkennen und die Dinge sauber zu halten. Lenker können übertrieben sein, wenn Sie auf Ihrer Website nicht so viel tun. Vielleicht möchten Sie in Underscore nach einfachen Dingen wie diesem suchen. Es wäre einfach und schnell zu implementieren.

Sie müssten nur ein paar Dinge ändern Vorlagen unterstreichen zu verwenden.Zuerst in Ihrer Seite unterstreichen schließen, dann einen Skript-Tag für die Vorlage hinzufügen Teils:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"/> 
<script type="text/template" class="template"> 
    <li> 
     <input id="<%- uid %>" type="checkbox" class="todo-item"> 
     <label for="<%- uid %>"></label><%- labelText %><i class="glyphicon glyphicon-remove todo-item-del"></i> 
    </li> 
</script> 

ändern Sie Ihren Click-Handler ein Datenobjekt für die Vorlage ein und verwenden Sie die Vorlage erstellen, um Ihre HTML-Zeichenfolge zu erstellen:

// _.template returns a function that is used to produce html string using whatever data you pass. 
var todoTemplate = _.template($('script.todoTemplate').html()); 

$('#update').on('click', function() { 
    var $myText, labelText, uid, templateData, html; 
    $myText = $('#myText'); 
    labelText = $myText.val(); 
    if (labelText.length === 0) { 
     alert('Are you Kidding ? ;)'); 
     return; 
    } 
    // create the unique id for the label 
    uid = new Date().getTime(); 
    // create the template data object 
    templateData = { 
     uid: uid, 
     labelText: labelText 
    }; 
    // use your template function to produce the html string you will append 
    html = todoTemplate(templateData); 

    $('#result').append(html); 

    // update value 
    $myText.val('').change(); 
}); 

ich eine jsfiddle erstellt haben um es zu testen, ob Sie ein funktionierendes Beispiel sehen möchten: https://jsfiddle.net/n7bwymno/1/

Verwandte Themen