jquery
  • function
  • 2014-12-05 44 views 0 likes 
    0

    我需要創建一個jquery函數並在onClick()中調用此函數,但我需要在很多按鈕中使用此函數,所以我無法使用該按鈕的ID。我怎麼能這樣做?如何在OnClick事件中調用Jquery函數?

    這是我所需要的想法。

    的Jquery:

    ShowModalBT = function(nameModal){ 
        $("#"+nameModal).modal("show");        
    }; 
    

    HTML:

    <input type='button' onClick='ShowModalBT("a")'> 
    
    +0

    '$( '按鈕')。點擊(函數() {//你的代碼在這裏}) – j08691 2014-12-05 14:44:52

    回答

    1

    使用jQuery你可以使用任何CSS選擇器,然後一些。只需將一個類添加到想要具有此功能的按鈕並編寫適合您的代碼即可。例如:

    $('.MyButtons').on('click', function() { 
        //Click Function Code Here 
    }); 
    
    +0

    謝謝,現在我的功能正在工作。 – 2014-12-05 15:33:24

    0

    如果它是你打電話,你可以使用類的每個按鈕的功能相同,是這樣的:

    <input type="button" class="modalButton">

    $('.modalButton').click(function(){ 
    
        var id = $(this).attr('id'); 
        //do stuff with id 
    
    }); 
    
    0

    你可以嘗試添加一個類你的輸入元素,並從jquery調用它:

    <input type='button' class="xxx"> 
    

    然後在jQuery中我們Ë

    $('.xxx').on('click',function(){ 
        $(this).modal("show"); 
    }); 
    
    0

    沿東西這行:

    <input type='button' class='triggerModal' id='a'> 
    <input type='button' class='triggerModal' id='b'> 
    

    然後在你的腳本:

    $('.triggerModal').click(ShowModalBT); 
    
    function ShowModalBT(event) { 
        var arg = $(event.target).attr('id'); 
        .... 
    } 
    
    0

    似乎要打開不同的按鈕點擊不同的模式。

    您可以嘗試這種方式,只需將模式ID的data-attributes添加到按鈕。

    $('input[type="button"]').click(function (event) { 
     
        var modalName = $(event.currentTarget).attr('data-modal-name'); 
     
        $('div').text('Opening modal '+ modalName); 
     
        //$("#"+modalName).modal("show");  
     
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> 
     
    <input type='button' data-modal-name = "a" value="Open Modal A"> 
     
    <input type='button' data-modal-name = "b" value="Open Modal B"> 
     
    <input type='button'data-modal-name = "c" value="Open Modal C"> 
     
    
     
    <div></div>

    1

    一類添加到選擇&觸發模式上的click事件

    $(document).on('click','.btn',function(){ 
    $("#myModal").modal(); 
    }) 
    

    JSfiddle Example for Modal

    相關問題