[英]check if field value has changed with jquery
我想不斷檢查值是否發生變化..所以如果用戶在輸入表單中選擇一個新值,我想運行一些 js 代碼..
<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");
});
首先,你不應該在一個頁面上有 2 個具有相同 ID 的元素。 改用類
<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
。
或者,給你的<input class="select2-focusser select2-offscreen"
和<select name="billing_country"..
一類someClass
和聽的變化
$('.someClass').change(function()
{
alert("change");
});
嘗試指定其中的所有輸入:
$('.select2-chosen').find(':input').change(function()
{
alert("change");
});
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.