简体   繁体   English

如何使用jQuery获取元素中的所有输入?

[英]How do I get all the inputs in a element with jQuery?

I'm new to jQuery. 我是jQuery的新手。 I'm trying to get all inputs inside a div element. 我试图将所有输入都放在div元素中。 Here is what I have coded so far: 这是我到目前为止编码的内容:

$(".pros_earnings_delete_annual_earning").click(function(e) {
    e.preventDefault();
    var target = $(e.target);
    var parent = target.parent(".total-for-earnings");
    var inputs = parent.children(":input");
    console.log(inputs);
    $.each(inputs, function(index, value) {
        console.log(value);
    });
});

When I click I get this: 当我点击我得到这个:

[prevObject: v.fn.v.init[0], context: button.pros_earnings_delete_annual_earning, selector: ".parent(.total-for-earnings).children(:input)", constructor: function, init: function…]

The each method does not seem to output anything. 每种方法似乎都没有输出任何东西。

HTML: HTML:

<div class="total-for-earnings" style='background-color:#ccc;padding:10px;overflow:hidden;'>
    <div style='width:160px;float:left;'><strong>Total for 2013:</strong> 
        <input type='hidden' name='pros_earnings_annual_year[]' value='2013'>
    </div>
    <input type='text' name='pros_earnings_annual_amount_mul[]' placeholder='0' value='10' size='8' style='width:80px;' />
    <select name='pros_earnings_annual_amount_sup[]' style='width:110px;'>
        <option value='Thousand'>Thousand</option>
        <option value='Million' selected>Million</option>
        <option value='Billion'>Billion</option>
    </select><span>USD</span>
    <div style="float:right;">
        <button class="pros_earnings_save_annual_earning" style="margin-left:15px; width:auto; height:25px;">Save</button>
        <button class="pros_earnings_delete_annual_earning" style="margin-left:15px; width:25px; height:25px;">-</button>
    </div>
</div>

Simply put: 简单的说:

var $inputs = $(':input',parent)

Will match all inputs in the context of the parent element. 将匹配父元素上下文中的所有输入。

You're console.log is outputting the jQuery object which represents each item in this collection, if you want the value of the input, just ask: 你是console.log输出的jQuery对象代表了这个集合中的每个项目,如果你想要输入的值,只需要问:

$inputs.each(function(){
    console.log($(this).val());
})

Also, there was one other bug - target.parent(..) should have been target.parents(..) (plural) 另外,还有一个bug - target.parent(..)应该是target.parents(..) (复数)

Live example: http://jsfiddle.net/3aJuZ/ 实例: http//jsfiddle.net/3aJuZ/

Change 更改

var target = $(e.target);
var parent = target.parent(".total-for-earnings");
var inputs = parent.children(":input");  

to, 至,

var parent = $(this).closest(".total-for-earnings");
var inputs = parent.find(":input");  //Unlike children(), find() will go any depth to match.

the problem is in this line 问题出在这一行

var parent = target.parent(".total-for-earnings");

change to 改成

var parent = target.parents(".total-for-earnings");

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

相关问题 将jquery-ui自动完成功能所属的对象/ dom元素应用于所有输入后,如何引用它 - How do I reference the sepcific object/dom element that an jquery-ui autocomplete function belongs to, when it has been applied to all inputs 如何使用jQuery获取所有文本输入值? - How to get all values of text inputs with jQuery? JavaScript / jQuery:如何在XML元素中获取所有属性的数组? - JavaScript/jQuery: How do I get an array of all attributes in an XML element? 如何使用jquery切换所有子元素的readonly属性 - How do I toggle the readonly attribute of all child element with jquery 如何使用 JavaScript 或 jQuery 获得跨度中三个输入值的总和? - How do I get the sum of three inputs values in a span using JavaScript or jQuery? 如何获取 DOM 元素的所有属性和方法 - How do I get all the properties and methods of a DOM element 如何获取使用 jQuery 单击的动态加载的元素 - How do I get the dynamically loaded element that was clicked with jQuery 使用jQuery选中时,如何获取元素的常规属性? - How do I get normal attributes of an element when selected with jQuery? 在jQuery(或Javascript)中,如何获得“最顶层”元素? - In jQuery (or Javascript), how do I get the “topmost” element? 如何使用jQuery在给定位置获得最高元素? - How do I get the top most element at a given location with jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM