使用 jquery 检测任何选定的下拉值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12912513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
detecting any selected dropdown value with jquery?
提问by Joe
I have a function that detects all input value changes with keyup:
我有一个函数可以用 keyup 检测所有输入值的变化:
Jquery:
查询:
// detect all inputs
$('.inputChange').each(function() {
// Save current value of element
$(this).data('oldVal', $(this).val())
// Look for changes in the value
$(this).bind("propertychange keyup input paste", function(event){
// If value has changed...
if ($(this).data('oldVal') != $(this).val()) {
// Updated stored value
$(this).data('oldVal', $(this).val())
currentVal = $(this).val()
// Do action
inputElement = $(this).attr('data-element')
inputProperty = $(this).attr('data-property')
inputUnit = $(this).attr('data-unit')
updateAllCSS(inputElement, inputProperty, currentVal + inputUnit)
}
});
});
HTML:
HTML:
<input class="inputChange" data-element=".homeTemplate #colaAlpha h1" data-property="font-size" data-unit="px">px
I now need to do another that will get the selected dropdown values from this HTML:
我现在需要做另一个从这个 HTML 中获取所选下拉值的操作:
<select class="selectChange" data-element=".homeTemplate #colaAlpha" data-property="height" data-unit="px">
<option value="8">8px</option>
<option value="12">12px</option>
<option value="21">21px</option>
</select>px
I was wondering how I should go about doing this? is there perhaps a propertychange event that will work?
我想知道我该怎么做?也许有一个propertychange 事件会起作用?
回答by Geert
Have a look at http://api.jquery.com/change/:
看看http://api.jquery.com/change/:
$(".selectChange").change(function (){
alert($(this).val());
});
回答by Joe
Unlike the above complex input detection code, here is the select code that will work for any of the selects on my page- it uses the selected value and also the data tags:
与上面复杂的输入检测代码不同,这里的选择代码适用于我页面上的任何选择——它使用选定的值和数据标签:
$('.selectChange').change(function() {
updateAllCSS($(this).attr('data-element'), $(this).attr('data-property'), $(this).val() + $(this).attr('data-unit'))
});