मैं एक प्रणाली सरल टैग बॉक्स है, समस्या यह है कि अगर मैं एक ही शब्द जोड़ने के लिए,टैग के डुप्लिकेशन से कैसे बचें?
उदाहरण:
php, और php,
लेबल एक ही शब्द के साथ दोहराया गया है ।
कोड पूर्ण।
$(function(){ // DOM ready
// ::: TAGS BOX
$("#tags input").on({
focusout : function() {
var txt = this.value.replace(/[^a-z0-9\+\-\.\#]/ig,''); // allowed characters
if(txt) $("<span/>", {text:txt.toLowerCase(), insertBefore:this});
this.value = "";
},
keyup : function(ev) {
// if: comma|enter (delimit more keyCodes with | pipe)
if(/(188|13)/.test(ev.which)) $(this).focusout();
}
});
$('#tags').on('click', 'span', function() {
$(this).remove();
});
});
#tags{
float:left;
border:1px solid #ccc;
padding:5px;
font-family:Arial;
}
#tags > span{
cursor:pointer;
display:block;
float:left;
color:#fff;
background:#789;
padding:5px;
padding-right:25px;
margin:4px;
}
#tags > span:hover{
opacity:0.7;
}
#tags > span:after{
position:absolute;
content:"×";
border:1px solid;
padding:2px 5px;
margin-left:3px;
font-size:11px;
}
#tags > input{
background:#eee;
border:0;
margin:4px;
padding:7px;
width:auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="tags">
<input type="text" value="" placeholder="Add a tag" />
</div>
समस्या सिर्फ लेबल (टैग) के दोहराव, एक शब्द की तरह एक ही जोड़ना है।
आप क्या करने के लिए जब उपयोगकर्ता एक नकली टैग को जोड़ने की कोशिश करता है चाहते हैं? – Calum