2010-08-24 182 views
24

我有這樣的HTML看起來像這樣:JQuery的/ JavaScript的 - 觸發按鈕從另一個按鈕單擊事件單擊

<input type="submit" name="savebutton" class="first button" /> 
<input type="submit" name="savebutton" class="second button" /> 

和JS:

jQuery("input.second").click(function(){ 
    // trigger second button ? 
    return false; 
}); 

所以,我怎麼能觸發從Click事件點擊第一個第二個按鈕? 請注意,我沒有對第二個按鈕,既不在HTML或在其上單擊事件的任何控制...

+0

你說你無法控制第二個按鈕,爲什麼?它是否在基於不同域的'iframe'中? – 2010-08-24 19:38:41

+0

不,它在WordPress的核心,我不想修改它的代碼 – Alex 2010-08-24 19:41:05

回答

59

添加ID的兩個輸入端,身份證= 「第一」 和id = 「第二」

//trigger second button 
$("#second").click() 
+0

謝謝,沒想到會那麼簡單:) – Alex 2010-08-24 19:41:41

8

你的意思是這樣的:

jQuery("input.first").click(function(){ 
    jQuery("input.second").trigger('click'); 
    return false; 
}); 
18

嗯,你剛剛火所需的單擊事件:

$(".first").click(function(){ 
    $(".second").click(); 
    return false; 
}); 
15
jQuery("input.first").click(function(){ 
    jQuery("input.second").trigger("click"); 
    return false; 
});