简体   繁体   English

事后得到唯一的身份证?

[英]Get unique id after the fact?

I want to be able to change a button image on click which isn't the issue. 我希望能够在点击时更改按钮图像,这不是问题。 Each button is unique to information that is pulled from the database and on click the button should change and send the appropriate info to the database. 每个按钮对于从数据库中提取的信息是唯一的,单击该按钮应该更改并将相应的信息发送到数据库。 What I am unsure about is how I can change the specific button. 我不确定的是如何更改特定按钮。 Say there are five things up, so five buttons. 说有五件事,所以五个按钮。 Each button has its unique id to go with its information. 每个按钮都有唯一的ID来与其信息一起使用。 How can I find out what the id is so that I can manipulate the button? 我如何找出id是什么,以便我可以操作按钮?

I use php to grab the info from the database and go through a while loop to get it all displayed. 我使用php从数据库中获取信息并通过while循环将其全部显示出来。 Once it is all displayed it is there for the user to see and then they can click on the button. 一旦全部显示,它就在那里供用户查看,然后他们可以点击按钮。

edit - It just came to me. 编辑 - 它刚来找我。 I can make my onclick function take a variable and feed the variable into it. 我可以使我的onclick函数接受一个变量并将变量输入其中。 Right? 对?

A simple trick actually. 实际上是个简单的伎 Call a function on the click and pass its id, to it as a parameter. 在click上调用一个函数并将其id作为参数传递给它。

<button id="id1" onClick="handleclick(this.id)">Button1</button>
<button id="id2" onClick="handleclick(this.id)">Button2</button>

<script>
function handleclick(id)
{
    alert(id); //here is your id
}
</script>

Demo 演示


Alternative 替代

You can do something like this also 你也可以这样做

buttons = document.getElementsByTagName("button");
for( var x=0; x < buttons.length; x++ ) {
    buttons[x].onclick = handleclick;
}

Demo 2 演示2

Same idea as Starx, but alternative style: 与Starx相同,但替代风格:

<button id="id1">Button1</button>
<button id="id2">Button2</button>

<script>
    function handleclick() {
        alert(this.id); //here is your id
    }

    document.getElementById("id1").onclick = document.getElementById("id2").onclick = handleclick;
</script>

Just for the sake of argument - how to do it without javascript onclicks making your HTML look untidy, etc. 只是为了争论 - 如何在没有javascript onclicks的情况下做到这一点,让你的HTML看起来不整洁等等。

Add you event listeners in JavaScript in the HTML head or an include file, all wrapped in a document ready function, so avoiding javascript in your HTML body. 在HTML头部或包含文件中添加JavaScript中的事件侦听器,所有这些都包含在文档就绪函数中,因此避免在HTML正文中使用javascript。 Something like: 就像是:

window.onload = function(){
 for (i=1;i<=10;i++){ //assuming you'll never have more than 10 buttons
      var el = document.getElementById('id' + i);
      if (el != null){ //set click event handler if button exists
           document.getElementById('id' + i).addEventListener('click', handleClick, false);
      }else{
           break; //stop looking when a button doesn't exist.
      }
 }
};

You could do that less hackishly using the form object rather than getElementById, but you hopefully get my point. 你可以使用表单对象而不是getElementById来减少hackishly,但你希望得到我的观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM