开发者

CodeIgniter/JQuery/Ajax: Set dropdown based on previously selected value

开发者 https://www.devze.com 2023-03-13 02:11 出处:网络
I know this has been asked in various ways, but I have not been able to find a clear answer to my problem.I\'m using a combo if CI/JQuery/Ajax for CRUD operations within my app.Within my create are mu

I know this has been asked in various ways, but I have not been able to find a clear answer to my problem. I'm using a combo if CI/JQuery/Ajax for CRUD operations within my app. Within my create are multiple dropdowns, which are populated from the database...the ID of each user selection is stored with the new record upon saving. When a user updates that record, I need to be able to render the update page, with the dropdowns populated, but set to the selected value. Any guidance you can provide on where in the app/how I can set this is greatly appreciated. Below are snippets of the code I am using:

JQuery:

$.ajax({
url: 'index.php/admin/getEventById/' + updateId,
dataType: 'json',            
success: function( response ) {
$( '#uEventName' ).val( response.event_name );
$( '#uEventType' ).val( response.eventType_eventType_id ); /*Obviously, this would work fine if I was setting the value of a textbox but this is the ID# of the value I want to have my dropdown default to */

My View:

 <p>
    <label for="uEventName">Event Name:</label>
     <input type="text" id="uEventName" name="uEventName" />
  </p>
  <p>
     <label for="uEventType">Event Type:</label>   <!--I'm populating the dropdown with all values, but need it to be set to value as obtained above (via the response) -->
            <?开发者_如何学Gophp        
                $eventtype_options = array();
                foreach($eventtypes as $eventtype){
                $eventtype_options[$eventtype->eventtype_id]=$eventtype->event_type;
                }
                echo "<td>" . form_dropdown('uEventType', $eventtype_options) . "</td>";
            ?>         
            </p>  


Possibly this in your jquery?

$('td select[name=uEventType]').val('"'+response.event_name+'"');

From here

0

精彩评论

暂无评论...
验证码 换一张
取 消