简体   繁体   中英

Display dynamically created text box on dynamically created radio button with JavaScript

I am receiving some data via json and based on the inputs I have dynamically created a table that displays pre-selected radio buttons (Green / Yellow / Red) based on the values in the input. In the third column, I am displaying a comments box.

Now, I only want to display the comment box only if yellow / red radio button comes as input or if I select.

Here is my code in use:

 $(document).ready(function() { var appStatus = [{ "category": "Overall Status", "status": "0" }, { "category": "System Availability", "status": "1" }, { "category": "Whatever", "status": "2" }]; var tr; for (var i = 0; i < appStatus.length; i++) { tr = $('<tr/>'); tr.append("<td>" + appStatus[i].category + "</td>"); tr.append('<tr id="row' + i + '"><td><input type="radio" name="inlineRadioOptions_' + i + '[]" value="0" id="inlineRadio0"' + (appStatus[i].status == '0' ? ' checked="checked"' : '') + '><font color="black">Grey &emsp;</font><label class="radio-inline"><input type="radio" name="inlineRadioOptions_' + i + '[]" value="1" id="inlineRadio1"' + (appStatus[i].status == '1' ? ' checked="checked"' : '') + '><font color="green">Green &emsp;</font><label class="radio-inline"><input type="radio" name="inlineRadioOptions_' + i + '[]" id="inlineRadio2" value="2"' + (appStatus[i].status == '2' ? ' checked="checked"' : '') + '><font color="yellow">Yellow &emsp;</font></label><label class="radio-inline"><input type="radio" name="inlineRadioOptions_' + i + '[]" id="inlineRadio3" value="3"' + (appStatus[i].status == '3' ? ' checked="checked"' : '') + '> <font color="red">Red</font></label><td></tr>'); //tr.append("<td>" + "<input>" + appStatus[i].comments + "</input>" + "</td>"); tr.append("<td>" + '<tr id="row' + i + '"><td><input type="text" id="appStatus[i].comments" name="appStatus[i].comments" placeholder="Comments" class="form-control name_list" required' + "</td>"); $('table').append(tr); } $('#result').on('click', function() { var new_status = []; $('.table tbody tr').each(function() { new_status.push({ category: $(this).find('td:eq(0)').text(), status: $(this).find(':radio:checked').val() }); }); console.log(new_status); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-hover"> <thead> <th>Category</th> <th>Status</th> <th>Comments</th> </thead> </table> 

Add handlers to each radio input. Since you are using jQuery, the attached event handlers have this as the event source. You must separate your longer <tr>...</tr> blocks in order to achieve this.

A simple example:

table.append('<tr/>').append(
    $('<td>').append(
        $('<input type="radio" name="x">').change(function () {
            $(this).closest('td').find('input.comments').toggle(true or false);
        })
    )
)

...but you can be smarter and write this much clearer using variables.

There's also that you are appending <tr> elements inside another in your code, which leads to invalid HTML. Plus some other small glitches that need to be worked on.

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