简体   繁体   English

如何获得按钮单击事件上输入的当前值

[英]how to get the current values of an input on button click event

I have a bunch of div's that have the same functionality and I'm trying to write a function for them. 我有一堆具有相同功能的div,我正在尝试为它们编写一个函数。

Basically they have a predetermined value and a user input value and those need to be multiplied. 基本上,它们具有预定值和用户输入值,并且需要相乘。

I've looked through a bunch of other questions and this is the closest one I could find. 我研究了其他问题, 是我能找到的最接近的问题。 Almost exactly, but none of those answers work. 几乎完全正确,但这些答案均无效。

Any help would be great. 任何帮助都会很棒。 Thanks in advance! 提前致谢!

<div>
   <label>enter value for multiple here</label>
   <input type="text" class="multiple" factor="foo1"/>
   <button type="button" class="multiplyBtn">Click here to multiply these numbers</button>

   <label>enter value for multiple here</label>
   <input type="text" class="multiple" factor="foo2"/>
   <button type="button" class="multiplyBtn">Click here to multiply these numbers</button>
</div>

Here's the JS: 这是JS:

$('.mulitplyBtn').click(function() {
   var factor = $(this).closest('attr.factor').val();
   var multiple = $(this)closest('.multiple').val();
   answer = (factor * multiple);
};

This would work: 这将工作:

 $('.multiplyBtn').click(function() { // you had multiplyBtn spelled wrong here var cur = $('.multiplyBtn').index($(this)); // get index of clicked btn var factor = $('.multiple').eq(cur).data('factor'); // get factor from matching input var multiple = $('.multiple').eq(cur).val(); // get value from matching input answer = (Number(factor) * Number(multiple)); // make sure both are numbers then multiply alert(answer); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <label>enter value for multiple here</label> <input type="text" class="multiple" data-factor="4" /> <button type="button" class="multiplyBtn">Click here to multiply these numbers</button> <br> <label>enter value for multiple here</label> <input type="text" class="multiple" data-factor="7" /> <button type="button" class="multiplyBtn">Click here to multiply these numbers</button> </div> 

In the first line you used " and ' try using the same quotes (I mean using ".multiplyBtn" or '.multiplyBtn' ) 在第一行中,您使用了“和”,并尝试使用相同的引号(我的意思是使用“ .multiplyBtn”或“ .multiplyBtn”)

Second time, 3rd line you didn't use any quote when calling .multiple. 第二次,第三行,您在调用.multiple时不使用任何引号。 So turn it in that format : var multiple = $(this)closest('.multiple').val() 所以以这种格式打开它:var multiple = $(this)closest('。multiple')。val()

let me know the result 让我知道结果

attr.factor does not work like this. attr.factor不能这样工作。

try it like this: 尝试这样:

$('.multiplyBtn').click(function() {
    var container = $(this).prev('.multiple'); //don't forget the "dot"
    var multiple = container.val();
    var factor = container.attr('factor'); //since it is the same container. 
    var answer = (factor * multiple); //what exactly are you tryin to multiply?
};

You have at least two typos and are using the wrong jQuery function. 您至少有两个错字,并且使用了错误的jQuery函数。

Typos: 错别字:

  • $('.mulitplyBtn') should be $('.mulitplyBtn') $('。mulitplyBtn')应该是$('。mulitplyBtn')
  • $(this)closest should be $(this).closest $(this)closest应该是$(this).closest

Wrong function: 功能错误:

closest() searches the parents, and the only parent here is the DIV with no class. close()搜索父级,并且这里唯一的父级是没有类的DIV。 What you probably want is to use parent() to go up to the DIV, then find() to search the parent's children for a specific element: 您可能想要的是使用parent()上至DIV,然后使用find()在父级的子级中搜索特定元素:

$(this).parent().find('.multiple').val()

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

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