单击时使用jQuery获取输入值 [英] On click get input value using jQuery

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

问题描述

我有以下内容:

$('.checkbox').click(function () {
      console.log(this);
      $.ajax({
        type:'POST',
        url: '/loadProducts',
        data: {},
        success: function(response) {
               console.log(response);
               $('.js-products').html(response);
        }});
    return false;
});

现在执行console.log(this)的地方,它返回:

Now where I do the console.log(this), it returns:

<div class="ui checkbox checked">
    <input type="checkbox" name="gender[Women]" tabindex="0" class="hidden">
    <label>Women</label>
</div>

如何获取输入名称(性别)?以及该复选框是否已签出?

How do I get the input name (gender)? And whether the checkbox is checked out or not?

推荐答案

您可以使用jQueryfind方法获取输入对象,然后检查是否已检查性别女性,可以使用prop jQuery的方法.

You can use the method find of jQuery to get the input object, then to check if the gender woman is checked you can use prop method of jQuery as well.

$('.checkbox').click(function () {
  // to get the input
  var $input = $(this).find('input');
  // to check if the checkbox is checked or not
  console.info($input.prop('checked'));

            $.ajax({
                type:'POST',
                url: '/loadProducts',
                data: {},
                success: function(response) {
                    console.log(response);
                    $('.js-products').html(response);
                }});
    return false;
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ui checkbox checked">
        <input type="checkbox" name="gender[Women]" tabindex="0" class="hidden">
        <label>Women</label>
 </div>

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

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