简体   繁体   English

从动态表单获取更大价值的输入

[英]Get input with bigger value from dynamic form

I have the below HTML and I want to use jQuery to find out all the input elements with an id beginning with a that have a value bigger than id beginnging with b . 我有下面的HTML,我想使用jQuery找出ida开头的所有input元素,其值大于idb开头的id For this example, the result below would select it will be #a2 and #a3 . 对于此示例,下面的结果将选择它为#a2#a3

<div class="form">
    <input id="a1" value="1" />
</div>
<div class="form">
    <input id="b1" value="5" />
</div>

<div class="form">
    <input id="a2" value="3" />
</div>
<div class="form">
    <input id="b2" value="1" />
</div>

<div class="form">
    <input id="a3" value="2" />
</div>
<div class="form">
    <input id="b3" value="1" />
</div>

<div class="form">
    <input id="a4" value="1" />
</div>
<div class="form">
    <input id="b4" value="5" />
</div>

...

You can use .filter() with condition that compares both values: 您可以将.filter()与可比较两个值的条件一起使用:

$('[id^=a]').filter(function(){
   return parseInt($(this).val(),10) > parseInt($(this).parent().next().find('input').val(),10) 
});

Working Demo 工作演示

Update: To get the id of elements that matches the condition 更新:获取与条件匹配的元素的ID

$('[id^=a]').filter(function(){
    return parseInt($(this).val(),10) > parseInt($(this).parent().next().find('input').val(),10) 
}).map(function(){
    return $(this).attr("id");
}).get().join("-")

Demo 演示

You can use the 'attribute begins with' selector and filter() to achieve this: 您可以使用“属性始于”选择器和filter()来实现:

var $a = $('[id^="a"]').filter(function() {
    var aVal = parseInt($(this).val(), 10);
    var bVal = parseInt($(this).closest('.form').next().find('input').val(), 10);
    return aVal > bVal;
});

The $a variable will now contain the a elements with a higher value than their immediate sibling b . $a变量现在将包含a元素,其值比其直接同级b更高。

How to get all the number on the end of id a="" into an array? 如何获取ID a =“”结尾的所有数字到数组中?

To do this you can use map() : 为此,您可以使用map()

var aValues = $a.map(function() {
    return this.value;
}).get();
console.log(aValues); // shows all the larger A values gathered.

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

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