使用htmlfor获取输入值 [英] Get input value using htmlfor

查看:80
本文介绍了使用htmlfor获取输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完成这样的最佳方法是什么?

What’s the best way to accomplish something like this:

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

JavaScript

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

如果单击输入(未隐藏),我会得到值,但如何使用htmlfor来实现呢?有可能吗?

If I click the input, when not hidden, I get the value but how to achieve this using htmlfor? Is it possible?

推荐答案

您可以使用jQuery属性选择器和next()方法执行此操作,如下所示.

You can use jQuery attribute selector and next() method to do this like following.

$("[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" />

这篇关于使用htmlfor获取输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆