简体   繁体   中英

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

I have an application where it has a list of records. I have to bind to div dynamically. I have to add a textbox for every record.

Now for every selected record, I have to get what the user entered the value in textbox also.

I want to get the selected person email address (whatever the user keyed in the textbox for that selected record)

<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>

I want to get the selected person email address (whatever the user keyed in the textbox for that selected record)

You can use Javascript or Jquery

Javascript

<!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>

You need a dynamic id for every field or div.

Html Code

<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 function

$('.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);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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