我不是前端开发人员,而且我对JQuery JS等的接触非常有限。所以任何帮助都表示赞赏,谢谢。
目标: - 我想将字段集的所有字段(除了最后一个)的总和放入该字段集的最后一个字段中。
表单相当复杂,在这种形式下,有多个字段集,每个字段集包含多个 字段。
此表单由Drupal Views&我在此表单中导航的选项非常有限。
我不能使用id,name或任何其他属性,因为它们是由CMS动态生成的,也有数百个这样的字段集,因此不可能为每个字段集编写代码。
因此,未来添加的字段可能会改变属性。我无法在此表单中添加自己的属性,类或ID。
表单看起来像这样
<fieldset>
------<div>
---------<fieldset> **this one**
------------------<input>
------------------<input>
------------------<input>
------------------<input>
----------------nth <input> <= Sum of 1st to (n-1)th should come here
-----------------------<div>
我认为可以通过input:last
完成,但无法弄清楚如何正确使用它。
这是我写的函数
$(function() {
$('input').change(function() {
var sum = 0;
// Loop through all inputs
$(this).closest('fieldset').find('input').each(function() {
sum += parseInt($(this).val());
console.log(sum);
// i want to put sum into last <input> tag of the current fieldset
});
});
}); // end function()
console.log的输出($(this).closest(&#39; fieldset&#39;)。find(&#34; input&#34;)。last()););是
所以,我认为我必须提取属性值,到目前为止我已经尝试过这些
$(this).closest('fieldset').find("input").last().data('value'); Undefined
和
$(this).closest('fieldset').find("input").last().attr('value'); results in Undefined