0

I have a form with a select box and a input field: when option value of select is "8" I would have that input field must be less than 250

选择框

<select id="select_5" size="1" title="Effettua una scelta!"  name="tipo_nolavorazioni" onChange="updatethis(this.form);">
<option value="8"> A </option>
<option value="1"> B</option>
<option value="2"> C</option>
<option value="3"> D </option>
</select>

输入框

<input class="required validate-number" maxlength="150" size="5" id="text_2" name="base" type="text" onChange="updatethis(this.form);" value="" />
<p id="errore_base"></p>

在 JS 文件中,我添加了以下内容:

function updatethis(form) {
if (tipo1==8) {
window.addEvent('domready', function(){
  var valid = new LiveValidation('text_2', {insertAfterWhatNode : "errore_base"});
  valid.add(Validate.Numericality, { maximum: 250, tooHighMessage: "Max 250cm" });
  });
}
}

这可行,但如果我更改选项值(例如 B=1).. 错误消息仍然存在,我无法处理我的表单。我哪里错了?

4

1 回答 1

1

这行得通吗?我从来没有亲自使用过这个LiveValidation库,但是在查看了 API 之后,我想这会起作用。您需要在函数之外的某个地方(在更高的范围内)存储对验证器的引用,并使用addandremove方法来控制 UI。

有关更多信息,请参阅:http: //livevalidation.com/documentation#LiveValidationRemove

var validator = new LiveValidation('text_2', {insertAfterWhatNode : "errore_base"})

function updatethis(form) {
  var action = 'remove';

  if (tipo1 == 8) {
    action = 'add';
  }

  window.addEvent('domready', function(){
    validator.add(Validate.Numericality, { maximum: 250, tooHighMessage: "Max 250cm" });
  });
}
于 2015-01-07T15:52:32.290 回答