2012-06-28 3 views
1

Ici j'ai besoin de vérifier un clic de bouton en utilisant javascript i.e) si le bouton A est cliqué je vais appeler une fonction javascript et si le bouton B est cliqué, je vais appeler une autre fonction javascript.Comment vérifier si un bouton a été cliqué avec javascript?

if(document.getElementById('imgBTNExportPPT').clicked == true) 
{ 
    ShowDialogExportPPTPOPUP(); 
} 
else if(document.getElementById('btnShowModal').clicked == true) 
{ 
    ShowDialogPrintPOPUP(); 
} 

et

 <asp:ImageButton ID="imgBTNExportPPT" runat="server" Width="15" Height="15" border="0" 
                  OnClick="imgBTNExportPPT_Click" ImageUrl="~/Images/PPT_icon.png" /> 
    <asp:ImageButton ID="btnShowModal" runat="server" Width="15" Height="15" border="0" 
                 ImageUrl="~/Images/Print_icon.png" onclick="btnShowModal_Click" /> 

est-il possible ?? toute suggestion ??

+0

un coup d'oeil à ma réponse à jour. –

+0

Copie possible: http://stackoverflow.com/questions/2788191/is-there-a-way-to-use-javascript-to-check-if-a-button-has-been-clicked –

Répondre

5

Essayez ceci:

function buttonClicked(choice) 
{ 
    if(choice == 'A') 
    { 
     ShowDialogExportPPTPOPUP(); 
    } 
    else if(choice == 'B') 
    { 
     ShowDialogPrintPOPUP(); 
    } 
} 

code HTML devrait ressembler à ceci:

<input type='button' value='ButtonA' onclick="buttonClicked('A')" /> 
<input type='button' value='ButtonB' onclick="buttonClicked('B')" /> 

Si c'est un contrôle côté serveur, vous pouvez le faire de deux façons:

<asp:ImageButton onClientClick="buttonClicked('A')" ID="imgBTNExportPPT" runat="server" Width="15" Height="15" border="0" 
                  OnClick="imgBTNExportPPT_Click" ImageUrl="~/Images/PPT_icon.png" /> 
    <asp:ImageButton onClientClick="buttonClicked('A')" ID="btnShowModal" runat="server" Width="15" Height="15" border="0" 
                 ImageUrl="~/Images/Print_icon.png" onclick="btnShowModal_Click" /> 

OU (en C#)

{ 
imgBTNExportPPT.Attributes.Add("onclick", "buttonClicked('A')"); 
btnShowModal.Attributes.Add("onclick", "buttonClicked('B')"); 
} 

http://msdn.microsoft.com/en-us/library/7a9d6h4f(v=vs.80).aspx

+0

: c'est un asp contrôle de bouton alors je dois appeler celui-ci dans onclientclick ?? – Rooney

+0

oui, vous devrez utiliser la propriété onclientclick. –

+0

Merci. Tout le meilleur. –

2

Vous pouvez ajouter un identifiant ou tout simplement appeler une fonction dans l'événement onclick. Par exemple:

<script type="text/javascript"> 
    function onButtonClick(button) 
    { 
     alert("button " + button + " clicked!"); 
    } 
</script> 

<button id="ButtonA" onclick="javascript:onButtonClick('buttonA'); return false;" /> 
<button id="ButtonB" onclick="javascript:onButtonClick('buttonB'); return false;" /> 
3

Vous devez utiliser OnClientClick attribut de ImageButton pour attribuer la fonction javascript. Vous pouvez le passer en appel javascript pour obtenir un clic sur l'objet du bouton.

OnClientClick = "TestClick ('A', this);" ajouter ce bouton dans

Dans .aspx page

<asp:ImageButton OnClientClick="TestClick('A', this);" ID="imgBTNExportPPT" runat="server" Width="15" Height="15" border="0"                 OnClick="imgBTNExportPPT_Click" ImageUrl="~/Images/PPT_icon.png" /> 

<asp:ImageButton OnClientClick="TestClick('B', this);" ID="btnShowModal" runat="server" Width="15" Height="15" border="0" 
                   ImageUrl="~/Images/Print_icon.png" onclick="btnShowModal_Click" /> 

Dans Script

function TestClick(choice, btnClicked) 
{ 
    alert(btnClicked.id + " clicked"); 
    if(choice == 'A') 
    { 
     ShowDialogExportPPTPOPUP(); 
    } 
    else if(choice == 'B') 
    { 
     ShowDialogPrintPOPUP(); 
    } 
} 
2

Voici un bon exemple. Transférez votre code comme le rappel à runOnce et créer un gestionnaire d'événements onclick comme celui ci-dessous pour restreindre votre fonction à un appel:

function runOnce(callback) { 

    var done = false; 

    return function() { 

    if (!done) { 
     done = true; 

     callback(); 

    } 

    }; 

} 

var one = runOnce(function() { 
    alert('Once'); 
}), 
    two = runOnce(function() { 
    alert('Once only too.'); 
}); 

document.getElementById('first').onclick = one; 
document.getElementById('second').onclick = two; 

Voici une DEMO

Questions connexes