1

有没有办法我基本上可以做到这一点?

 var a= $(".object-type").change(function() 
 {
    //somecode
 }

然后调用 a(); 在另一个功能中

$("#selectAll").change(function () {
                    if($(this).attr("checked"))
                    {
                        a();
                    }
                    else
                    {
                      //some code
                    }
4

4 回答 4

4

你可以这样做

$(".object-type").change(function() 
 {
    //somecode
 });


 $("#selectAll").change(function () {
   if(this.checked)
    {
         $(".object-type").trigger('change'); //Trigger the change event of a different element.
        // or just $(".object-type").change();
   //some code
 });

示例演示

于 2013-05-29T04:16:59.633 回答
1
var a= function() 
 {
    //somecode
 }

$(".object-type").change(a);

$("#selectAll").change(function () {
                    if($(this).attr("checked"))
                    {
                        a();
                    }
                    else
                    {
                      //some code
                    }
于 2013-05-29T04:14:09.293 回答
1

如果你只是想模拟 的change事件$(".object-type"),为什么不试试jQuery 提供的触发事件。

$("#selectAll").change(function () {
 if($(this).prop("checked"))
 {
     $(".object-type").trigger("change");
     //you could also use this 
     $(".object-type").change();
 }
 else
 {
    //some code
 } });
于 2013-05-29T04:23:13.173 回答
0

It's not entirely clear what you're asking here. If you want two different jquery triggers to utilize the same functionality then you want to extract the callback function from the jquery trigger:

var a = function(event){//do something};
$('.class1').change(a);
$('.class2').change(a);

when class1 or class2 are changed the same function will be called.

If what you want if for one jquery trigger to execute another one then you can litterally trigger it:

$('.class1').change(function(event){//do something});
$('.class2').change(function(event){
    $('.class1').trigger('change');
    // do something else
});
于 2013-05-29T04:21:35.527 回答