原型JS,帮助检测表单元素的变化

时间:2010-09-20 08:52:08

标签: javascript prototypejs

我不熟悉原型,但我们的任务是对颜色选择器进行一些更改。我做了大部分改动而没有太多问题。

我需要做的是听取表格输入。

有人可以就如何做到这一点提出建议吗?

我找到了这个,但我不明白如何实现它:http://api.prototypejs.org/dom/form/element/observer/

这是我在插件中使用的元素。

实例一

//
// picker sample text
//
this.textValue = document.createElement('input');
this.textValue.type = "text";
this.textValue.name = "textValue";
this.textValue.className = options.textValueClass;
this.header.appendChild(this.textValue);

实例2

setColor: function(color) {
  this.textValue.setAttribute('value', color);
  this.sample.style.backgroundColor = color;
},

我希望Instance 3能够像这样。

if (this.textValue.<<changes>> && this.textValue.<<value.length>> == 7) {
  // Check if valid hex
  // Trigger Save
}

有人可以帮忙填写<< >>

1 个答案:

答案 0 :(得分:0)

请参阅http://www.prototypejs.org/api/event/observe

this.observe('change', this.validateOnChange.bind(this));

validateOnChange: function(event) {
    var value;
    value = $(this.fieldid).getValue();
    if (value.length == 7) {
        //Check if valid hex
        // Trigger Save
    }
}