2014-02-12 110 views
0

當用戶更改選擇選項輸入時,我一直試圖爲我的表單動態更改所需規則的消息。我似乎無法讓任何東西正常工作。選擇更改後,消息永遠不會改變。jQuery驗證:使用選擇選項更改所需消息

我嘗試使用功能變化中的選擇輸入,像這樣我的方法:

$('#selector').change(function(){ 
    Method X; 
} 

方法我試過:

  1. ​​

  2. $('#inputx').rules("remove", "required");

    $('#inputx').rules("add", { messages: {required: "New Message"}});

我也曾嘗試對消息的依賴作用,但是,這並不在所有的工作:即使unfocusing的#inputx元素

required: { 
    depends: function(element){ 
     if($('#selector option:selected').text() == 'Option1'){ 
      return "Message 1"; 
     } else{ 
      return "Message 2"; 
     } 
     return; 
    } 
} 

該錯誤信息不會改變

編輯:我在我的onChange內部做了一個不正確的條件語句。一切都完美了!

+0

哪裏是你的HTML標記?你的代碼的其餘部分在哪裏? – Sparky

回答

1

.rules('add')方法是工作的罰款對我來說...

http://jsfiddle.net/2NJS7/

$(document).ready(function() { 

    $('#myform').validate({ 
     rules: { 
      test: { 
       required: true 
      }, 
      myselect: { 
       required: true 
      } 
     } 
    }); 

    $('select[name="myselect"]').on('change', function() { 
     if ($(this).val() == 1) { 
      $('input[name="test"]').rules('add', { 
       messages: { 
        required: "this is the new message" 
       } 
      }); 
     } 
    }); 

}); 
+0

哇,工作!謝謝Sparky!試圖圍繞這個問題做些什麼來解決我的問題有很多麻煩。 – Conner

+0

@Kronus,我很高興它的工作,但既然你沒有顯示你的完整代碼,我仍然不知道你錯在哪裏。也許你可以編輯你的OP來顯示你的失敗嘗試,這樣在閱讀時可以幫助其他人。 – Sparky

相關問題