简体   繁体   English

使用Jquery复制多个字段的值

[英]Using Jquery to copy the values of multiple fields

I'm trying to write a jquery funciton that will copy specified fields from the element above the current one into matching fields of the current element. 我正在尝试编写一个jquery函数,它将指定字段从当前元素上方的元素复制到当前元素的匹配字段中。 Specifically I have a asp.net repeater that is spitting out tables with a few fields in it and I want a "Same as above" function. 具体来说,我有一个asp.net转发器,它正在吐出带有几个字段的表,我想要一个“与上面相同”的功能。 I'm still new to jquery though and I'm having some trouble with it. 我仍然是jquery的新手,我遇到了一些麻烦。 Here is some psudo code for what I'm currently trying to do feel free to propose a better method if you know it or simply fix this to work. 这里有一些psudo代码,用于我目前正在尝试做的事情,如果你知道它或者只是简单地修复它就可以提出更好的方法。

function CopyPrevious(sender, rowId) {
    var current = $(sender).closest('#wrappingDiv').find('.containingTable').eq(rowId);
    var previous = $(sender).closest('#wrappingDiv').find('.containingTable').eq(rowId - 1);
    $(current).find('.fieldA').val($(previous).find('.fieldA').val());
}

wrappingDiv is just a div I put around the table so I could find it with "closest" and each table has the class "containingTable". wrappingDiv只是我放在桌子周围的一个div,所以我可以找到它“最近”,每个表都有“containsTable”类。 I put "fieldA", "fieldB", etc as class names on the fields so I could find them to get the values. 我把“fieldA”,“fieldB”等作为类名放在字段上,这样我就可以找到它们来获取值。

The issue I'm having is a javascript error on row 4 of the above: $current is not defined 我遇到的问题是上面第4行的javascript错误:$ current未定义

EDIT: Updated the line 4 per the comments. 编辑:根据评论更新了第4行。 It works now. 它现在有效。 Thank you. 谢谢。

You cannot use .val() as an lvalue . 您不能将.val()用作左值 To set a new value pass it as an argument instead: 要设置新值,请将其作为参数传递:

$(current).find('.fieldA').val($(previous).find('.fieldA').val());

Not precisely sure I follow, but would this work for you? 不确定我是否遵循,但这对你有用吗?

function CopyPrevious(sender, rowId) {
    var current = $(sender).closest('.wrappingDiv').find('.containingTable input');  
    var previous = $(sender).closest('.wrappingDiv').prev().find('.containingTable input');
    current.each(function(){
        ($this).val(previous.is('.' + $(this).attr('class')).val());
    });
}

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

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