将值列表添加到值本身,不包括其自身的值
Adding a list of values to the value itself excluding its own value
我在父div中有一个文本框列表,比如'parentDom'。我需要将一个数据属性"list"附加到这些文本框中的每一个,该文本框具有parentDom下所有文本框的值列表,除了它自己。
到目前为止,我有这个
$('input').each(function(index, item) {
var list = _.pluck(
_.reject(
$('input'),
function(obj) {
return $(obj).data('id') == $(item).attr('id')
}
),
'name');
$(item).data('list', list);
});
我使用下划线.js。这是正确的方法吗?
一种方法(假设我正确解释问题)是:
// getting a reference to the relevant <input /> elements:
var inputs = $('#parentDom input');
// iterating over each of those <input /> elements, with attr():
inputs.attr('data-value', function() {
// creating a reference to the current <input /> over which we're iterating:
var cur = this;
return inputs.filter(function() {
// filtering the jQuery collection to remove the current <input />
return this !== cur;
}).map(function() {
// converting the (string) value to a number, using the
// unary plus operator:
return +this.value;
// creating an array of the values
}).get().reduce(function(a, b) {
// reducing the array of values to a single value,
// by summing the previous and current values together:
return a + b;
});
});
// inserting a <span> after the <input /> elements,
// just to show the result:
inputs.after(function() {
return '<span>' + this.dataset.value + '</span>';
});
input {
display: inline-block;
float: left;
clear: left;
margin: 0 0 0.4em 0;
}
span {
float: left;
margin-left: 0.5em;
}
span::before {
content: '(data-value: ';
}
span::after {
content: ').';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parentDom">
<input type="text" value="1" />
<input type="text" value="2" />
<input type="text" value="3" />
<input type="text" value="4" />
<input type="text" value="5" />
<input type="text" value="6" />
<input type="text" value="7" />
</div>
然而,一种更简单的方法是:
// getting a reference to the relevant <input /> elements:
var inputs = $('#parentDom input'),
// finding the total of *all* the <input /> elements:
totalValue = inputs.map(function() {
return +this.value;
}).get().reduce(function(a, b) {
return a + b;
});
// iterating over the <input /> elements with attr():
inputs.attr('data-value', function() {
// setting the data-value attribute to the total value
// of all the <input /> elements minus the value of the
// current <input />:
return totalValue - (+this.value);
});
// inserting a <span> after the <input /> elements,
// just to show the result:
inputs.after(function() {
return '<span>' + this.dataset.value + '</span>';
});
input {
display: inline-block;
float: left;
clear: left;
margin: 0 0 0.4em 0;
}
span {
float: left;
margin-left: 0.5em;
}
span::before {
content: '(data-value: ';
}
span::after {
content: ').';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parentDom">
<input type="text" value="1" />
<input type="text" value="2" />
<input type="text" value="3" />
<input type="text" value="4" />
<input type="text" value="5" />
<input type="text" value="6" />
<input type="text" value="7" />
</div>
引用:
- JavaScript:
-
Array.prototype.reduce()
.
-
- j查询:
-
attr()
. -
get()
. -
map()
.
-
相关文章:
- 接受不在列表中的值-引导组合框
- 如何将页面上的所有电话号码更改为可点击链接,不包括特定类别的元素
- 将正则表达式重新分解为不包括空组
- 为什么从JSON文本到类型的转换不包括函数
- XMLHttpRequest调用不包括If Modified Since标头
- 使用 JavaScript 执行一行 PHP(不包括 PHP 文件)
- 捆绑器不包括 .min 文件
- 与冒号(不包括冒号)之间的正则表达式单词匹配
- 包含特殊字符 (!,@,#,&) 不包括其他特殊字符
- Codrops页面转换.如何创建单独的按钮,而不是列表项目的效果
- Javascript RegEx:如何获取单引号之间的名称(不包括单引号)
- JQuery在链接的静态页面中搜索,突出显示找到的单词,而不破坏列表
- Javascript RegEx匹配URL's,但不包括图像
- Rails不包括用于javascript的require_tree
- Ui自动完成不显示列表
- r.js - 不包括特定的文件列表
- 将值列表添加到值本身,不包括其自身的值
- javascript表单验证,不包括隐藏的下拉列表
- 计算不包括关键字列表的字符串的长度
- 过滤($filter)从对象列表中匹配值,不包括不必要的属性