[英]jQuery setting input using val() and activating change()
I have a large number of color selectors that input the value into a input field: 我有大量的颜色选择器,它们将值输入到输入字段中:
<input type="text" class="colour_picker"/>
Now when I prepopulate the fields, I want to use something like: 现在,当我预填充字段时,我想使用类似以下的内容:
$(document).on("change", ".colour_picker",function(){
$(this).css("background-color", "#"+$(this).val());
});
However, this does not fire, I fugured out that I would need to call .change() after setting .val(), however is there an easier way to do this? 但是,这不会触发,我搞砸了我需要在设置.val()之后调用.change(),但是有更简单的方法吗? I have coded thousands of lines of JS for this and don't really feel like going through them all finding all of the pre-population scripts and modifying them.
我已经为此编写了数千行JS代码,并真的不想遍历它们全部找到所有预填充脚本并进行修改。
EDIT: Here's a fiddle to show an example of the problem. 编辑:这是一个小提琴,以显示问题的示例。 http://jsfiddle.net/kj5sjs22/1/
http://jsfiddle.net/kj5sjs22/1/
Onchange only fires when a user types a text and, then, the input lose focus. 仅当用户键入文本,然后输入失去焦点时,才会触发Onchange 。 It's not your case.
这不是你的情况。 You must trigger the event.
您必须触发事件。
$(".colour_picker").val("000000").trigger('change');
Regards. 问候。
Try 尝试
$(document).on("change", ".colour_picker",function(){
$(this).css({"background-color":"#"+$(this).val()});
});
or 要么
$(".colour_picker").keyup(function(){
$(this).css({"background-color":"#"+$(this).val()});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.