2014-04-29 34 views
-1

我試圖獲得選擇框的值,但它始終爲我帶來第一個值。選擇框未正常工作後初始化

在開始時,select是空的。我按下一個顯示模式的按鈕並加載選擇框的信息。我想,問題就來了,因爲我直到按鈕被按下,因爲我與另一個選擇嘗試,效果不錯

我不加載選擇選項是這樣的模式:

<div class="modal fade" id="myModalPremios" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
       <h4 class="modal-title" id="myModalLabel">Premios</h4> 
      </div> 
      <div id="premios" class="modal-body"> 
       <jsp:include page="../frag/premios-list-frag.jsp"/> 
      </div> 
      <div class="modal-footer"> 
       <button class="btn btn-danger" aria-hidden="true" data-dismiss="modal">Ok</button> 
      </div> 
     </div> 
    </div> 
</div> 

這是形式該模式中:

<form class="form-horizontal" role="form"> 
    <div class="form-group"> 
     <input type="hidden" id="id" name="quiniela.id" value="${quiniela.id }"/> 
    </div> 
    <div class="form-group"> 
     <label for="plantillaOwnerQuiniela" class="col-sm-4 control-label">Quiniela</label> 
     <div class="col-sm-5"> 
      <select class="form-control" name="quiniela.plantillaOwnerQuiniela" id="plantillaOwnerQuiniela" ${read }> 
       <c:forEach items="${lista2 }" var="pl"> 
        <option value="${pl.id }" 
        <c:if test="${pl.id == quiniela.plantillaOwnerQuiniela.id}">selected="selected"</c:if>>${pl.nombrePlantillaOwner} - ${pl.precioQuiniela}Bs</option> 
       </c:forEach> 
      </select> 
     </div> 
    </div> 
</form> 

這是我的顯示模式:

$("#myModal").modal("show"); 

我已經讀過,也許該元素不存在於Dom,然後我試着使用這個函數來看看是否發生了什麼,但它不起作用。

$('#plantillaOwnerQuiniela').on('change',function() { 
    var selectVal = $('#plantillaOwnerQuiniela :selected').val(); 
    alert(selectVal); 
}); 
+0

我使用的引導模式3。我真的無法弄清楚如何來試試你的答案跟我的代碼,我告訴我的模式diferently – xpiero

+0

它不會工作,因爲我正在使用的struts2也..我填充選擇框數據調用一個動作,當我顯示模式 – xpiero

回答

0

我在嘗試讀取if的值時通過包含模態體類來解決了我自己的問題。就像這樣:

$('.modal-body #plantillaOwnerQuiniela :selected').val(); 
0

只是一個猜測,但它似乎像plantillaOwnerQuiniela元素不存在在你的綁定監聽change時間DOM。

嘗試以下操作:

$("body").on("change", "#plantillaOwnerQuiniela", function(e){ 
    var selectVal = $(e.currentTarget).val(); 
    alert(selectVal); 
}); 

使用.on()到聽者重視body並將其委派到選擇,對連接到選擇本身;這可能會或可能不會出現在頁面加載中。

+0

我認爲你是對的,因爲選擇開始是空的,然後我按下按鈕創建,它加載信息到窗體,然後顯示模式。但這並沒有工作 – xpiero

+0

我試過這個,它不起作用。 – paulalexandru

0

你應該更改事件綁定到這樣的酥料餅的初始化方法:

$('.button').popover({ 
    html : true, 
    content: function() { 
     return $('#popover_content_wrapper').html(); 
    }     
}).parent().delegate('#plantillaOwnerQuiniela', 'change', function() { 
    alert($(this).val()); 
}); 

Here就是例子!