2015-06-26 46 views
-1

我想conastantly檢查值的變化。所以,如果用戶在輸入表格中選擇新的價值我想運行一些js代碼改變..校驗字段值與jQuery

<div class="select2-container country_to_state country_select" id="s2id_billing_country" style="width: 100%;"> 
    <a href="javascript:void(0)" class="select2-choice" tabindex="-1"> 
     <span class="select2-chosen" id="select2-chosen-1">Schweiz</span> 
     <abbr class="select2-search-choice-close" /> 
     <span class="select2-arrow" role="presentation"> 
    <b role="presentation"/> 
    </span> 
    </a> 
    <label for="s2id_autogen1" class="select2-offscreen">Land *</label> 
    <input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-1" id="s2id_autogen1" /> 
</div> 
<select name="billing_country" id="billing_country" class="country_to_state country_select " tabindex="-1" title="Land *" style="display: none;"> 
    <option value="">Land auswählen…</option> 
    <option value="DE" selected="selected">Deutschland</option> 
    <option value="CH">Schweiz</option> 
    <option value="AT">Österreich</option> 
</select> 

我已經嘗試過目前爲止(不起作用)

$('#select2-chosen-1').change(function() { 
    alert("change"); 
}); 
+0

我想同一個id問題。您可以更改編號 –

+1

'因此,如果用戶在輸入表單中選擇了新值'但是您已經發布了與'span'元素相關的HTML標記,而不是'input'。因此,如果這是您的相關HTML標記,那麼您如何更改'span'內容? –

+0

請檢查onchange事件是否存在span, – Robin

回答

1

首先,你不應該有一個頁面上具有相同ID的2個元素。使用類代替

<span class="select2-chosen">Value 1</span> 
<span class="select2-chosen">Value 2</span> 

$('input').on('change', '.select2-chosen', (function() 
{ 
    alert("change"); 
}); 

在這裏,我們不得不實施對輸入的其中獲得通過動態頁面select2實例化之後產生的下放事件偵聽器。然後我們聽聽它的change事件和alert

另外,給出someClass<input class="select2-focusser select2-offscreen"<select name="billing_country"..一類,並聽取了變化

$('.someClass').change(function() 
{ 
    alert("change"); 
}); 
+0

span'元素沒有默認事件'onchange' –

+0

等待,可能是錯誤的HTML標記,因爲OP在討論'input ',所以不知道期待什麼:) –

+0

我會在一秒內編輯它,併發布整個表單對不起。 –

0

嘗試之內,而不是指定所有的輸入:

$('.select2-chosen').find(':input').change(function() 
{ 
alert("change"); 
}); 

希望這有助於