jquery.tagsinput.js實(shí)現(xiàn)記錄checkbox勾選的順序
業(yè)務(wù)需求:可以根據(jù)checkbox的先后勾選傳遞有順序的值讓后臺(tái)接收,決定用tagsinput顯示checkbox的先后勾選順序,并實(shí)時(shí)響應(yīng)checkbox的勾選狀態(tài)
思路:checkbox的值存在一個(gè)數(shù)組A,新創(chuàng)建一個(gè)數(shù)組B,如果選中一個(gè),B push一個(gè)值,取消一個(gè),remove一個(gè)值,因?yàn)閿?shù)組是有序的,就做到響應(yīng)checkbox的勾選順序
效果:

代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Tagsinput Checkbox</title>
 
<link rel="stylesheet" type="text/css" href="jquery.tagsinput.css" >
</head>
 
 
<body>
 checkbox: <input type="checkbox" value="0" name="fruit" οnclick="radioHandle(this.value)" />Apple
 <input type="checkbox" value="1" name="fruit" οnclick="radioHandle(this.value)" />Banana
 <input type="checkbox" value="2" name="fruit" οnclick="radioHandle(this.value)" />Pear
 <input type="checkbox" value="3" name="fruit" οnclick="radioHandle(this.value)" />Orange
 <br><br>
 <input type="text" id="fruit-tags" name="fruit-tags" >
 <input type="text" id="fruitChecked">
 
<script src="jquery.min.js" ></script>
<script src="jquery.tagsinput.js" ></script>
<script type="text/javascript">
 var fruitValue = new Array();
 var fruitName = ['Apple','Banana','Pear','Orange'];
 
 Array.prototype.removeByValue = function(val) {
  for(var i=0; i<this.length; i++) {
  if(this[i] == val) {
   this.splice(i, 1);
   break;
  }
  }
 };
 
 
 $('#fruit-tags').tagsInput({
 interactive: false,
 removeWithBackspace: false,
 onRemoveTag: function(value){
  //重點(diǎn) 通過(guò)值獲得數(shù)組的索引
     //刪除 checkbox不勾選 數(shù)組remove值 顯示改變
     var index = fruitName.indexOf(value);
     $("input:checkbox[name=fruit][value="+index+"]").prop("checked",false);
     fruitValue.removeByValue(index);
     $('#fruitChecked').val(fruitValue.toString());
    }
 });
 
 function radioHandle(value){
  // --- 選中 push--
  if($("input:checkbox[name=fruit][value="+value+"]").is(':checked')){
   if($('#fruitChecked').val().indexOf(value) == -1){
    fruitValue.push(value);
    $('#fruit-tags').addTag(fruitName[parseInt(value)]);
   }
  }else{
   if($('#fruitChecked').val().indexOf(value) > -1){
    fruitValue.removeByValue(value);
    $('#fruit-tags').removeTag(fruitName[parseInt(value)]);
   }
  }
  $('#fruitChecked').val(fruitValue.toString());
 } 
 
 
</script>
</body>
</html>
資源:Demo
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
 jQuery實(shí)現(xiàn)內(nèi)容定時(shí)切換效果完整實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)內(nèi)容定時(shí)切換效果,以完整實(shí)例形式較為詳細(xì)的分析了jQuery結(jié)合時(shí)間函數(shù)針對(duì)內(nèi)容的定時(shí)切換顯示相關(guān)技巧,需要的朋友可以參考下2016-04-04
 jQuery實(shí)現(xiàn)IE輸入框完成placeholder標(biāo)簽功能的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)IE輸入框完成placeholder標(biāo)簽功能的方法,涉及jQuery事件響應(yīng)及針對(duì)頁(yè)面元素屬性的動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
 jQuery live( type, fn ) 委派事件實(shí)現(xiàn)
jQuery 1.3中新增的方法。給所有當(dāng)前以及將來(lái)會(huì)匹配的元素綁定一個(gè)事件處理函數(shù)(比如click事件)。也能綁定自定義事件。2009-10-10
 基于jquery的simpleValidate簡(jiǎn)易驗(yàn)證插件
簡(jiǎn)易驗(yàn)證插件simpleValidate使用心得,基于JQuery,修改小BUG后能兼容各瀏覽器,附自用版本供下載2014-01-01
 jquery實(shí)現(xiàn)彈出div,始終顯示在屏幕正中間的簡(jiǎn)單實(shí)例
本篇文章主要是對(duì)jquery實(shí)現(xiàn)彈出div,始終顯示在屏幕正中間的簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-03-03

