2016-09-06 53 views
0

我遇到了一個問題,我製作了一個小遊戲,在這裏您可以通過舞臺進入,並且在每個階段您都必須做出選擇。 我想保存用戶在「hola」和「chao」中選擇的一些信息,然後在幫助者中將這些信息保存在會話中。 但我不知道怎麼弄的 「HOLA」 價值---> {{fidelidadSegmento}}流星 - 從自定義屬性中獲取價值

<select class="form-control" id="select0" name="categ"> 
    <option value = "0" selected="selected"> Stay </option> 
     {{#each op in opciones}} 
     <option value="{{op.costo}}" hola="{{fidelidadSegmento}}" chao="{{op.nombre}}"> {{fidelidadSegmento}} con {{op.nombre}} , eficiencia: {{op.eficiencia}}</option>costo 
     {{/each}} 
</select> 

我在瀏覽器中看到的是:

<select id="select0" class="form-control" name="categ"> 
<option selected="selected" value="0"> Stay </option> 
<option value="3" hola="Awereness" chao="tv"> Awereness con tv , eficiencia: 0.8</option> 

<option value="2" hola="Awereness" chao="magazine"> Awereness con magazine , eficiencia: 0.7</option> 

</select> 

我想屬性「hola」和「chao」的值:console.log($(this).attr('hola'));讓我不確定 和$(event.target).find(「HOLA」)讓我的翻譯:

回答

0

如果你想使用jQuery然後嘗試訪問使用選定的選項:

$("select option:selected") 

但請記住,默認選項不具有'hola'或'chao'屬性。您將必須測試未定義的屬性,或將「hola」和「chao」屬性添加到默認選項。

// This will return undefined for the default option 
$("select option:selected").attr("hola"); 

post可能對你有幫助也。

+0

謝謝r00ster。這解決了我的問題。你好。 $(「select option:selected」)。attr(「hola」);完美地工作。 – Satjeet