jQuery onClick capture the id of the element

后端 未结 6 1061
借酒劲吻你
借酒劲吻你 2020-12-29 20:26

i have many input fields like this

 

when i click this rad

相关标签:
6条回答
  • 2020-12-29 20:59

    HTML:

    <input type="radio" name="name" id="name" onchange="enableTxt(this)" /> 
    

    JS:

    function enableTxt(elem) {
        var id = $(elem).attr("id");
        alert(id);
    }
    
    0 讨论(0)
  • 2020-12-29 21:02

    The html

     <button class="destroy" id="123">
    

    The jQuery

      $('button.destroy').on('click', function(e){
        e.preventDefault();
        console.log(this.id);
    
    0 讨论(0)
  • 2020-12-29 21:03

    Try this

    alert($("input:radio").attr('id'));
    
    0 讨论(0)
  • 2020-12-29 21:04

    pass this to the function

    enableTxt(this)
    
    function enableTxt(item) {
        var id = $(item).attr("id");
        alert(id);
    }
    
    0 讨论(0)
  • 2020-12-29 21:06

    Another option is with JQ

    $("#name").on('onchange',enableText);
    

    With this your object will be the current this

    0 讨论(0)
  • 2020-12-29 21:07

    You can pass just the ID from the HTML

    <input type="radio" name="name" id="name" onchange="enableTxt($(this).attr('id'))" />
    
    function enableTxt(id)
    {
        alert(id);
    }
    
    0 讨论(0)
提交回复
热议问题