简体   繁体   English

使用htmlfor获取输入值

[英]Get input value using htmlfor

What's the best way to accomplish something like this: 完成这样的最佳方法是什么:

html.erb html.erb

<!-- inside a loop -->
    <i htmlfor="wish">
        <!-- name or icon -->
    </i>
    <input type="hidden" class="wish-cls" name="wish" value="<%= product.id %>" />
<!-- end loop -->

JavaScript 的JavaScript

$(".wish").click(function(e){
  e.preventDefault();
  //var id = document.getElementsByTagName('wish-cls');
  console.log("Clicked: " + e.target.value);
});

If I click the input, when not hidden, I get the value but how to achieve this using htmlfor ? 如果我单击输入(未隐藏时), htmlfor得到该值,但如何使用htmlfor实现呢? Is it possible? 可能吗?

You can use jQuery attribute selector and next() method to do this like following. 您可以使用jQuery属性选择器和next()方法执行此操作,如下所示。

 $("[data-id=wish]").click(function(e){ e.preventDefault(); var value = $(this).next(".wish-cls").val(); console.log("Clicked: " + value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <i data-id="wish">Click</i> <input type="hidden" class="wish-cls" name="wish" value="1000" /> 

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

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