> php教程 > PHP开发 > 본문

jQuery 플러그인 확장 예시 [콜백 함수 추가]

高洛峰
풀어 주다: 2016-12-05 10:06:42
원래의
1172명이 탐색했습니다.

이 기사의 예에서는 jQuery 플러그인 확장 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.

<script language="javascript" type="text/javascript">
function doSomething(callback) {
  // … 
  // Call the callback
  callback(&#39;stuff&#39;, &#39;goes&#39;, &#39;here&#39;); // 给callback赋值,callback是个函数变量
}
function foo1(a, b, c) {
  // I&#39;m the callback
  alert(a + " " + b + " " + c);
}
doSomething(foo1); // foo1函数将使用callback函数中的数据 stuff goes here
var foo2 = function(a,b,c) {
  // I&#39;m the callback
  alert(a + " " + b + " " + c);
}
doSomething(foo2); // foo2函数将使用callback函数中的数据 stuff goes here
doSomething(function(a,b,c){
  alert(a + " " + b + " " + c); // function函数将使用callback函数中的数据 stuff goes here
});
</script>
로그인 후 복사

콜백 매개변수가 유효하려면 함수여야 합니다. 콜백 역할을 하기 위해서입니다.

<script language="javascript" type="text/javascript">
function doSomething(callback) {
  // … 
  // Call the callback
  if(typeof callback === &#39;function&#39;){
    callback(&#39;stuff&#39;, &#39;goes&#39;, &#39;here&#39;); // 给callback赋值,callback是个函数变量
  }else{
    alert(&#39;jb51.net&#39;);
  }
}
function foo1(a, b, c) {
  // I&#39;m the callback
  alert(a + " " + b + " " + c);
}
doSomething(foo1); // foo1函数将使用callback函数中的数据 stuff goes here
var foo2 = function(a,b,c) {
  // I&#39;m the callback
  alert(a + " " + b + " " + c);
}
doSomething(foo2); // foo2函数将使用callback函数中的数据 stuff goes here
doSomething(function(a,b,c){
  alert(a + " " + b + " " + c); // function函数将使用callback函数中的数据 stuff goes here
});
var foo3 = &#39;a&#39;;
doSomething(foo3);
</script>
로그인 후 복사

foo3이 함수가 아니면 jb51.net이 뜹니다

jQuery 인스턴스

원래 함수

$.fn.citySelect=function(settings)
로그인 후 복사

콜백 추가

$.fn.citySelect=function(settings, changeHandle) // 添加回调函数changeHandle
로그인 후 복사

콜백 함수에 값 할당

//选项变动赋值事件
var selectChange = function (areaType) {
  if(typeof changeHandle === &#39;function&#39;){ // 判断callback是否是函数
    var prov_id = prov_obj.get(0).selectedIndex;
    var city_id = city_obj.get(0).selectedIndex;
    var dist_id = dist_obj.get(0).selectedIndex;
    if(!settings.required){
      prov_id--;
      city_id--;
      dist_id--;
    };
    if(dist_id<0){
      var data = {
        prov: city_json.citylist[prov_id].p,
        city: city_json.citylist[prov_id].c[city_id].n,
        dist: null
      };
    }else{
      var data = {
        prov: city_json.citylist[prov_id].p,
        city: city_json.citylist[prov_id].c[city_id].n,
        dist: city_json.citylist[prov_id].c[city_id].a[dist_id].s
      };
    }
    changeHandle(data, areaType); // 返回两个处理好的数据
  }
};
로그인 후 복사

도, 시, 군 데이터 데이터와 트리거된 변경 이벤트 유형을 가져옵니다. 각 이벤트

// 选择省份时发生事件
prov_obj.bind("change",function(){
    cityStart();
    selectChange(&#39;prov&#39;); // 返回数据
});
// 选择市级时发生事件
city_obj.bind("change",function(){
    distStart();
    selectChange(&#39;city&#39;); // 返回数据
});
// 选择区级时发生事件
dist_obj.bind("change",function(){
    selectChange(&#39;dist&#39;); // 返回数据
});
로그인 후 복사
프런트 엔드

을 사용하여 selectAgent 함수

$("#s_city").citySelect({
  prov: "江苏省",
  city: "宿迁市",
  dist: "宿城区",
  nodata: "none"
},
function(data, type) {
  selectAgent(data.city, data.dist);
});
로그인 후 복사
에 대한 콜백에서 반환된 데이터를 사용합니다.

ajax 데이터를 통해 해당 에이전트를 가져옵니다.

플러그인 수정이 완료되었습니다.

function selectAgent(city,district){
    $.ajax({
      type:"POST",
      url:"{sh::U(&#39;Index/ajax&#39;,array(&#39;todo&#39;=>&#39;getagent&#39;))}",
      data:"city="+city+"&district="+district,
      success:function(json){
        json = JSON.parse(json);
        opt_str = "<option value=&#39;&#39;>-请选择-</option>"
        if(json.status == 1){
          $.each(json.data,function(index,con){
            opt_str += "<option value="+con.id+">"+con.name+" 电话:"+con.tel+"</option>"
          })
        }
        $(&#39;#agent_id&#39;).html(opt_str);
      }
    });
}
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!