繁体   English   中英

如何从具有动态ID的div获取动态文本框值

[英]How to get dynamic textbox value from div with dynamic id's

我有一个应用程序,其中有一个记录列表。 我必须动态绑定到div。 我必须为每个记录添加一个文本框。

现在,对于每个选定的记录,我还必须获取用户在文本框中输入的值。

我想获取选定人员的电子邮件地址(无论用户在文本框中键入该选定记录的任何内容)

<div class="row">
        <input type="checkbox"> <label>Bob</label> Email: <input type="textbox"/>  
    </div>
    <div class="row">
        <input type="checkbox"> <label>Bob</label> Email: <input type="textbox"/>  
    </div>
    <div class="row">
        <input type="checkbox"> <label>Bob</label> Email: <input type="textbox"/>  
    </div>

    <button onclick="saveSelectedEmail()">Submit</button>

<script>
function saveSelectedEmail(){

}
</script>

我想获取选定人员的电子邮件地址(无论用户在文本框中键入该选定记录的任何内容)

您可以使用Javascript或Jquery

Java脚本

<!DOCTYPE html>
<html>
<body>

Name: <input type="text" id="myText" value="Mickey">

<p>Click the button to change the value of the text field.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
console.log(document.getElementById("myText").value)
  document.getElementById("myText").value = "Johnny Bravo";
}
</script>

</body>
</html>

您需要为每个字段或div输入一个动态ID。

HTML代码

<table id="userData" class="table table-striped table-bordered" width="100%">
    <thead>
        <tr>
            <th>Select</th>
            <th>Loan Id</th>
            <th>User</th>
            <th>Lender</th>
        </tr>
    </thead>
    <tbody>
        {% for emi in emis  %}
           <tr>
               <td><input type="checkbox" class="checkMe" name="checkBox" id="{{emi.userId._id.toString()}}"></td>
                <td>{{ emi._id.toString().slice(-6) }}</td>
                <td><a href="/admins/users/{{ emi.userId._id.toString() }}">{{ emi.userId.name }}</a></td>
                <td><a href="/admins/users/{{ emi.lenderId._id.toString() }}">{{ emi.lenderId.name }}</a></td>
           </tr>
        {% endfor %}
    </tbody>
</table>

Javascript功能

$('.checkMe').on('click', function() {
    var id = $(this).attr('id');

    if (arr.includes(id)) {
        let index = arr.indexOf(id);
        arr.splice(index, 1);
    } else {
        arr.push(id);
    }
});

暂无
暂无

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

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