有人可以幫助澄清嗎?我試圖做同樣的事情,這個post但動態設置值。但是,當我這樣做時,即使我在打開模態之前設置了數值,該值似乎沒有被設置。選擇選項不適用於Modal
我有一個新建活動按鈕,點擊它時,表的一部分,要求如下所示的JavaScript方法:
<th data-field="create_campaign"><span class="create_campaign" text="{{product.id}}"><a class="" href="#" data-target="campaign_modal" value="{{product.id}}">create new campaign</a></span><br></th>
HTML
<div id="campaign_modal" class="modal">
<div class="modal-content">
<blockquote>
Product Campaign
</blockquote>
<div id="" class="row">
<div class="input-field col s5">
<select id="product_selector" name="product_selector">
<option disable value=" ">Select a Product </option>
{% for product in all_org_products %}
<option value="{{product.id}}">{{product.name}}</option>
{% endfor %}
</select>
</div>
<div class="input-field col s5">
<input type="text" id="campaign" class=""/>
<label for="campaign" id="label_campaign"> Campaign </label>
</div>
<div class="input-field col s2">
<a class="btn-floating btn-small waves-effect waves-light teal" onclick="submitCampaign()"><i class="material-icons">add</i></a>
</div>
</div>
<div class=" center">
<span class="done_btn"><button id="done_btn" class="btn blue waves-effect waves-light modal-action"><i class="material-icons left" >done</i>Done</button></span>
</div>
</div>
</div>
的Javascript
$('.create_campaign').click(function() {
product_id = $(this).attr('text');
var product_select = document.getElementById('product_selector');
console.log(product_select);
var index=0;
for (var i=0; i<product_select.options.length; i++){
if (product_select.options[i].value == product_id){
index=i;
product_select.value=product_id
break;
}
}
product_select.selectedIndex = index;
openCampaignModal();
});
function openCampaignModal(){
$('#campaign_modal').openModal();
};
JavaScript運行時沒有錯誤,但是如何確保它在模態之上運行?目前,當點擊選項(創建新廣告系列)時,我首先設置select元素的值,然後才調用openModal。 – RajveerParikh
在
標記旁邊添加腳本或在您的script.js文件中包含 – jimmy我正在使用materializecss,因此我沒有任何
標記。此外,不知道如何才能知道選擇的價值,因爲有多個選項來「創建新的廣告系列」,並根據被點擊的選項,我希望選擇該選擇下拉列表中的值。 – RajveerParikh