Javascript 使用JQuery将值从表复制到模式

Javascript 使用JQuery将值从表复制到模式,javascript,jquery,html,onclick,buttonclick,Javascript,Jquery,Html,Onclick,Buttonclick,我有一张表,上面列出了我所有的数据。数据打印在for循环中,因此表中有许多行。单击按钮后如何获取行中的用户ID <table> <td> <input type="hidden" value="<c:out value="${user.id}" />" name="userId" /> </td> <td&g

我有一张表,上面列出了我所有的数据。数据打印在for循环中,因此表中有许多行。单击按钮后如何获取行中的用户ID

<table>
        <td>
            <input type="hidden" value="<c:out value="${user.id}" />" name="userId" />                          
        </td>
        <td>
        <button class="btn btn-default fileUpload" data-toggle="modal" id="btnUpload" data-target="#file-modal" value="<c:out value="${user.id}" />">Upload</button></td>
</table>
2.
这将在不更改html的情况下完成此操作

$(document).ready(function () {
    $("#btnUpload").click(function(){
        var $userId = $(this).parent().prev("td").children("input:hidden").val();
        $("#uploadUserId").val($userId);
    }); 
});
另一个解决方案是向输入元素添加ID,如下所示

  <input id="userVal" type="hidden" value="<c:out value="${user.id}" />"name="userId" /> 

这将在不更改html的情况下完成此操作

$(document).ready(function () {
    $("#btnUpload").click(function(){
        var $userId = $(this).parent().prev("td").children("input:hidden").val();
        $("#uploadUserId").val($userId);
    }); 
});
另一个解决方案是向输入元素添加ID,如下所示

  <input id="userVal" type="hidden" value="<c:out value="${user.id}" />"name="userId" /> 

这应该行得通。只需选择并获取用户Id txt,然后将其传递给uploadUserId值

$(document).ready(function () {
    $("#btnUpload").click(function(){
        var $userId = $('#userId').text();
        //$("#uploadUserId").val($userId);
        $('input[name="uploadUserId"]').val($userId);
    }); 
});
更新

嗯。我用你的HTML结构再次回顾了这一点,并把这一切都解决了。下面的代码可以工作

$('[type="submit"]').on('click', function(e){
    e.preventDefault(); // prevents the button default action. You can remove this if you want the form to submit after click.
    var $userId = $(this).prev().prev().val(), // this gets the input value. based on your html it is the previous previous element.
        fix1 = $userId.replace(/(?:\..+)$/, '').split('\\'), // this is a regex that replces the file extention then splits the string on the back slash turning it into an array.
        fix2 = fix1.length -1; // gets the length of the array and subtracts 1 to get the last array index of the array. This i to get the file name.

    $('[name="uploadUserId"').val(fix1[fix2]); // this assigns the uploadUserId value to the name of the file using the  array and the index.
});

这应该行得通。只需选择并获取用户Id txt,然后将其传递给uploadUserId值

$(document).ready(function () {
    $("#btnUpload").click(function(){
        var $userId = $('#userId').text();
        //$("#uploadUserId").val($userId);
        $('input[name="uploadUserId"]').val($userId);
    }); 
});
更新

嗯。我用你的HTML结构再次回顾了这一点,并把这一切都解决了。下面的代码可以工作

$('[type="submit"]').on('click', function(e){
    e.preventDefault(); // prevents the button default action. You can remove this if you want the form to submit after click.
    var $userId = $(this).prev().prev().val(), // this gets the input value. based on your html it is the previous previous element.
        fix1 = $userId.replace(/(?:\..+)$/, '').split('\\'), // this is a regex that replces the file extention then splits the string on the back slash turning it into an array.
        fix2 = fix1.length -1; // gets the length of the array and subtracts 1 to get the last array index of the array. This i to get the file name.

    $('[name="uploadUserId"').val(fix1[fix2]); // this assigns the uploadUserId value to the name of the file using the  array and the index.
});

为什么不输入隐藏元素的id并使用它从隐藏元素获取值field@Aravind我不明白你在说什么。可以更具体一点吗?为什么不输入隐藏元素的id,并使用它从隐藏元素中获取值field@Aravind我不明白你在说什么。你能说得更具体一点吗?我忘了提到主表正在运行for循环,方法是使用.parent.prevtd.chidlren。。。。它不检索ID,因为userVAl.val方法只获取第一个ID。您能帮助我吗?我忘了提到主表正在运行for循环,该方法使用.parent.prevtd.chidlren。。。。它无法检索ID,因为userVAl.val方法只获取第一个ID。您能帮我吗?对不起。我刚刚捕获了什么是id。我们需要按名称而不是id获取隐藏的输入。因为字段没有id。但是当我在for循环中打印数据时,有多行带有userId字段。我尝试了此操作,但它返回了错误的ID,如何获取所选行的用户ID?抱歉。我刚刚捕获了什么是id。我们需要按名称而不是id获取隐藏的输入。因为字段没有id。但是当我在for循环中打印数据时,有多行带有userId字段。我尝试了这个,但它返回了错误的ID,我如何才能获得所选行的用户ID?