【问题标题】:CodeIgniter/JQuery/Ajax: Set dropdown based on previously selected valueCodeIgniter/JQuery/Ajax:根据先前选择的值设置下拉列表
【发布时间】:2011-09-15 20:47:40
【问题描述】:

我知道有人以各种方式提出过这个问题,但我一直无法为我的问题找到明确的答案。如果 CI/JQuery/Ajax 在我的应用程序中用于 CRUD 操作,我正在使用组合。在我的创建中有多个下拉列表,它们是从数据库中填充的……每个用户选择的 ID 在保存时与新记录一起存储。当用户更新该记录时,我需要能够呈现更新页面,并填充下拉列表,但设置为所选值。非常感谢您提供的有关应用程序位置/如何设置的任何指导。以下是我正在使用的代码的 sn-ps:

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

我的观点:

 <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) -->
            <?php        
                $eventtype_options = array();
                foreach($eventtypes as $eventtype){
                $eventtype_options[$eventtype->eventtype_id]=$eventtype->event_type;
                }
                echo "<td>" . form_dropdown('uEventType', $eventtype_options) . "</td>";
            ?>         
            </p>  

【问题讨论】:

    标签: jquery ajax codeigniter select drop-down-menu


    【解决方案1】:

    在你的 jquery 中可能有这个?

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

    来自here

    【讨论】:

    • Goog Afternoon @jpea,您能否详细说明您的答案。链接失效了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多