2014-02-06 78 views
-2

我想call a button clickanother button。所以如果我點擊第一個按鈕 - 第二個按鈕應該觸發。我有以下代碼。jQuery通話按鈕點擊其他按鈕

HTML:

<input type="submit" name="savebutton" id="first" /> 
<a href="http://www.google.com/"> 
<input type="submit" name="savebutton" id="second" /> 
</a> 

的jQuery:

$(function(){ 
    $('#first').click(function() { 
    $("#second").trigger('click'); // $('#second').click(); wont work either 
    }); 
}); 

鏈接到我的小提琴:Fiddle

我不能找出IAM做錯了 - 可能有人請解釋它對我?

+1

我沒有看到分配給第二個按鈕的任何點擊事件。見http://jsfiddle.net/tewathia/y47AL/2/ – tewathia

+1

它工作正常http://jsfiddle.net/y47AL/4/ –

+1

還是你的意思是'點擊'的超鏈接?在這種情況下,嘗試:'location.href = $(「#second」)。parent()。attr('href');' – tewathia

回答

1

它的作品,但你沒有在第二個處理程序!單擊事件被解僱了,但你不說會發生什麼......

這裏是一個代碼示例和fiddle

$(function(){ 
    $('#first').click(function() { 
     alert('button clicked'); 
     $("#second").trigger('click'); 
    }); 

    $("#second").click(function() { 
     alert('button 2 clicked!'); 
    }); 
}); 
+0

謝謝 - 我的壞 - 我以爲我可以觸發鏈接點擊。 –

0

試試這個, Demo

$(function(){ 
    $('#first').click(function(e) { 
     alert('first') 
     $("#second").submit(); 

     e.preventDefault() 

    }); 
    $('#second').submit(function(e) { 
     e.preventDefault() 
     alert('second') 
    }); 
}); 
0

它應該工作,這是有效的代碼

$(function(){ 
    $('#first').click(function() { 
    $("#second").trigger('click'); // 
    }); 
}); 

Incase your but噸是動態的,那麼你可以嘗試

$('#first').on('click' ,function(){ 
    $("#second").trigger('click'); 
}) 
0

試試這個:

$('#first').click(function() { 
     $("#second").trigger('click'); 
}); 


$('#second').click(function() { 
    alert($(this).parent().attr('href')) 
    window.open($(this).parent().attr('href')); 
    //for opening in same window use window.location=$(this).parent().attr('href') 
}); 

Demo