简体   繁体   中英

Why Can't I Select Dynamically (PHP) Generated Options?

I am having an issue with a table where each cell contains an id'd input or select field. The cell contents are dynamically generated from a MySQL query. Everything works fine except for my dynamically generated SELECT drop downs. I am using the following code in a switch statement:

case 'crew_1':
case 'crew_2': 
    echo '<td><select class="'.classExt($types[$key]).' '. $key .'_c" id="' . $key . '--' . $idx .'">';
    echo '<option value="" selected="selected"></option>';
    while ($rowtech = mysqli_fetch_assoc($rtech)){
        echo '<option value="' . $rowtech['name'] . '">' . $rowtech['name'] . '</option>';
    } mysqli_data_seek($rtech,0);
    echo '<option value="TEST">TEST</option>';      
    echo '</select></td>'; break;

This creates the following HTML:

<td>
    <select class="varchar crew_1_c" id="crew_1--1">
        <option value="" selected="selected"></option>
        <option value="Name 1">Name 1</option>
        <option value="Name 2">Name 2</option>
        <option value="TEST">TEST</option>
    </select>
</td>

From this I can select the blank or 'TEST' option and it returns the correct value in a console.log, but if I choose one of the dynamically generated names, the box returns back to the default blank value. I get the same responses on Chrome and IE.

Where is my disconnect?

Included for the person requesting the script generating to output to console:

$('tbody select, tbody input').change(function(){
    console.log($(this).val());
});

OP, you're missing the name attribute. This works fine for me:

<form method="post" action="">
    <select class="varchar crew_1_c" id="crew_1:1" name="choice">
        <option value="" selected="selected"></option>
        <option value="Name 1">Name 1</option>
        <option value="Name 2">Name 2</option>
        <option value="TEST">TEST</option>
    </select>
    <input type="submit" value="Submit" name="submit" />
</form>

<?php
var_dump($_REQUEST);
?>

Found my issue. The JS request earlier had me dig back through my code to discover that I was setting my values toUpperCase() for submission for consistency, but that was breaking my value match on the drop down. I set my values to uppercase in the select options also and TA-DA!

Thanks for the help, guys. Sometimes it takes a fresh pair of eyes to point out the obvious.

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