简体   繁体   English

如何从JavaScript中的按钮获取特定值

[英]How to get a specific value from a button in javascript

There I have three buttons and when I click on one I want the specific value of that button 那里有三个按钮,当我单击一个按钮时,我想要该按钮的具体值

here's my code 这是我的代码

 jQuery(':button').click(function () { var checkBtn = document.getElementById("checkBtn"); console.log(checkBtn.value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn' value='apple'><i class='fa fa-check'></i></button> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn' value='linux'><i class='fa fa-check'></i></button> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn' value='windows'><i class='fa fa-check'></i></button> 

$(this).val() is what you are looking for. $(this).val()是您要寻找的。 Your code is only logging the value for a specific ( checkBtn ) button when clicked on any button. 您的代码仅在单击任何按钮时才记录特定( checkBtn )按钮的值。

This is the way to get the value of clicked button 这是获取被点击按钮价值的方法

 $('button').click(function () { console.log($(this).val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn' value='apple'><i class='fa fa-check'></i></button> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn' value='linux'><i class='fa fa-check'></i></button> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn' value='windows'><i class='fa fa-check'></i></button> 

Your code always looks for the button with the unique id of checkBtn . 您的代码始终会寻找具有唯一idcheckBtn的按钮。 It is invalid to have multiple elements with the same id (it defeats the purpose of id ) and so when jQuery goes looking for an element based on its id , it stops after finding the first one (because there shouldn't be any other matches), so you always get the same answer. 具有多个具有相同id元素是无效的 (这违背了id的目的),因此当jQuery根据其id查找元素时,它会在找到第一个元素后停止(因为不应有任何其他匹配项) ),因此您总是会得到相同的答案。

Instead, just use the keyword this (which is dynamic) to get a reference to the object that the event handler is currently running on: 相反,只需使用关键字this (它是动态的)来获取对事件处理程序当前正在其上运行的对象的引用:

 $(':button').click(function () { console.log(this.value); // Pure JavaScript way console.log($(this).val()); // jQuery way }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn1' value='apple'><i class='fa fa-check'></i></button> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn2' value='linux'><i class='fa fa-check'></i></button> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn3' value='windows'><i class='fa fa-check'></i></button> 

Here's another post of mine that discusses this and how it gets dynamically bound to various objects. 是我另一篇文章 ,讨论了this以及它如何动态绑定到各种对象。


Now, I would argue that you should set up your HTML differently than you have it now because, semantically, submit buttons serve only to facilitate the submission of the other form data, not to convey form data themselves. 现在,我认为您应该以与现在不同的方式来设置HTML,因为从语义上讲,提交按钮仅用于促进其他表单数据的提交,而不是自己传达表单数据。 Also, having multiple submit buttons can lead to accidental form submissions. 同样,具有多个提交按钮可能导致意外的表单提交。 So, for a cleaner and clearer UX, instead of 3 submit buttons that each carry a value to be submitted, I would create 3 radio buttons and have one submit button. 因此,对于更简洁的UX,我将创建3个单选按钮并具有一个Submit按钮,而不是每个都带有要提交的值的3个提交按钮。

 $('button').click(function () { // Now, instead of "this", we just use a selector that finds the checked radio button console.log(document.querySelector("input[name='checkBtn']:checked").value); // Pure JavaScript way console.log($("input[name='checkBtn']:checked").val()); // jQuery way }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label> <input type='radio' class='checkBtn' name='checkBtn' value='apple'><i class='fa fa-check'></i>Apple </label> <label> <input type='radio' class='checkBtn' name='checkBtn' value='linux'><i class='fa fa-check'></i>Linux </label> <label> <input type='radio' class='checkBtn' name='checkBtn' value='windows'><i class='fa fa-check'></i>Windows </label> <button type='submit'>Submit</button> 

You can use this or e.target to reference the button clicked. 您可以使用thise.target引用单击的按钮。 As it stands you are selecting the first button with id "checkBtn". 就目前而言,您正在选择ID为“ checkBtn”的第一个按钮。 As a sidenote it's not a good practice to have several elements with the same id on a page. 附带说明,在页面上包含多个具有相同id元素不是一个好习惯。

 jQuery(':button').click(function(e) { console.log(e.target.value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type='submit' class='checkBtn' name='checkBtn' value='apple'><i class='fa fa-check'></i></button> <button type='submit' class='checkBtn' name='checkBtn' value='linux'><i class='fa fa-check'></i></button> <button type='submit' class='checkBtn' name='checkBtn' value='windows'><i class='fa fa-check'></i></button> 

USe this.value where this is the current context 使用this.value哪里this是当前上下文

 var valArray = []; $('button').click(function() { if (valArray.indexOf(this.value) === -1) { valArray.push(this.value); console.log(valArray); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn' value='apple'><i class='fa fa-check'></i>apple</button> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn' value='linux'><i class='fa fa-check'></i>linux</button> <button type='submit' class='checkBtn' name='checkBtn' id='checkBtn' value='windows'><i class='fa fa-check'></i>windows</button> 

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

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