2015-01-08 75 views
-3

我希望能够在单击按钮后获取id的值,以便我可以使用它来引用特定的表。我如何使用jQuery获取ID

<input type="button" id="<?php echo $id; ?>" onclick="delBtn(<?php echo $id; ?>)"> 
<script>  
    function delBtn(btnX){ 

     var itemId = btnX; 
     alert(itemId); 
     var x; 
     var y =confirm('Are you sure you want to delete this item ?'); 

     if(y == true){ 
      x = 'You pressed Ok'; 
      console.log(x); 
      `$`("button[id='"+itemId+"']").closest("tr").effect("highlight", { 
      color:'#4BADF5'}, 400); 
      $("button[id='"+itemId+"']").closest("tr").fadeout();` 
     } 
    } 
</script> 
+1

'onclick =“delBtn('<?php echo $ id;?>')”'???但描述你的问题。控制台错误?为什么你需要在这里通过ID,我仍然没有得到它? –

+0

仅供参考,ID ***必须在文档上下文中是唯一的,听起来像您正在使用重复ID,具有相同ID的多个按钮。无论如何,所有这个问题没有多大意义 –

+0

@ A.Wolff我有一个数据库中的项目表,我想要使用jquery动态删除一行,但我必须唯一标识每一行,所以我添加了一个点击函数捕获行ID的按钮,然后我将使用行ID来删除行。 – cyrusolu

回答

0

这将适用于页面上的所有按钮。它会得到被点击的按钮,并返回按钮的ID属性值。所以你可以有5个按钮与所有不同的ID,当他们中的一个被点击时,它将返回被点击的按钮的ID。

$('button').click(function(){ 
    alert($(this).attr('id')); 
}); 

WORKING FIDDLE

+0

您能否编辑您的问题并解释解决方案? – Magnilex

+0

@Magnilex现在好了吗? – Refilon

0

如果按钮是动态添加使用事件委派:

$(document).on('click', '#your_id', function() { 
    var id = $(this).attr('id'); 
    return id; 
}); 
+0

返回值在哪里? – Teemu

0

试试这个

$(document).ready(function(){ 
$('button').click(function(){ 
    var id = $(this).attr('id'); 
    alert(id); 
}); 
}); 
+0

不能从点击的处理器返回值 –

1

为什么不使用 “ATTR” 获得ID

$('button').click(function(){ 
alert(this.id); // you can also use $(this).attr("id"); 
}); 
0

使用ID:

function delBtn(btnX){ //btnX is not id, it's parameter only 
var itemId = btnX.id; //so you need to use id 
alert(itemId); 
..... 

但是把这个中的onclick HTML:

onclick="delBtn(this)" 
+0

您不关闭功能... – Refilon

+0

将....在代码示例通常意味着“更多的代码在这里/在此处添加代码”等而不是忘记关闭功能 – atmd

+0

@ Bhoejendra Sah感谢您的评论,但我将如何使用jquery引用id来做这样的事情$(“我将在这里使用什么来引用ID”).closest(“tr”)。效果(“高光”,颜色:'#4BADF5'},400); – cyrusolu

0

试试这个..

<input type="button" id="<?php echo $id; ?>" class="yourclass"> 

    $(".yourclass").click(function(){ 
    var id=$(".yourclass").attr("id"); 
    alert(id); 
    }); 
0

如果需要,您可以存储该ID,以便在范围内访问。

<input type="button" class="js-item-id" id="<?php echo $id; ?>" value="Remove"> 

<script> 
    var itemId; 

    function delBtn() { 

     var x; 
     var y = confirm('Are you sure you want to delete this item ?'); 

     // If y is true... 
     if(y) { 

      x = 'You pressed Ok'; 
      console.log(x); 
      $("button[id='"+itemId+"']").closest("tr").effect("highlight", { 
      color:'#4BADF5'}, 400); 
      $("button[id='"+itemId+"']").closest("tr").fadeout(); 

      // Show me the ID! 
      alert('You deleted item ID #' + itemId + '.'); 
     } 
    } 

    $('.js-item-id').on('click', function() { 

     // Return and store 'this' ID. e.g, if the buttons id was '1234', 
     // it would return '1234'. 
     itemId = $(this).attr('id'); 

     // Now, call the @delBtn() function. 
     delBtn(); 
    }); 
</script> 

另外,我不知道你的意思是复制你的代码时,要做到这一点,但你有一个错字:

$("button[id='"+itemId+"']").closest("tr").fadeout();` 

你有一个结尾逗号。它应该是:

$("button[id='"+itemId+"']").closest("tr").fadeout();