2017-10-09 47 views
0

我正在爲可用性日曆的輸入數組創建自定義驗證規則。我有自定義驗證所有設置,問題是當驗證規則中有錯誤時,我無法弄清楚如何使其定位到正確的元素並突出顯示它,主要是因爲數組的名稱。它對我的例子遵循以下慣例:'availability.0'。問題是,Jquery不會將.0視爲有效名稱的一部分。Laravel驗證無法在JQuery AJAX中定位錯誤名稱

我使用AJAX來發送表單數據時出現錯誤,它返回該JSON數據爲例

{"readyState":4,"responseText":"{\"availability.0\":[\"Time set is not 
valid, please enter time in the following format 10:00AM-4:00PM. If you 
have\\r\\n   multiple blocks of time available in a day, separate by 
comma in the following format 10:00am-12:00pm,2:00pm-4:00pm\"]}","responseJSON": 
{"availability.0":["Time set is not valid, please enter time in the following 
format 10:00AM-4:00PM. If you have\r\n   multiple blocks of time 
available in a day, separate by comma in the following format 10:00am- 
12:00pm,2:00pm-4:00pm"]},"status":422,"statusText":"Unprocessable Entity"} 

這裏是JSONViewer照片顯示多個錯誤:

enter image description here

我不知道如何使這項工作,我不能在可用性的末尾添加.0,因爲jquery不承認有效,但如果我不這樣做,那麼它不是我需要的數組的名稱。我不知道該從哪裏出發。

回答

1

您可以使用方括號並放置您定位的字符串來選擇「availability.0」響應。

responseJSON["availability.0"] 

您可以在選擇可能具有JavaScript非法字符的對象屬性時使用此選項。如空格,數字,句號等。

Property accessors Add, Modify, or Access Object Literal Properties

+0

感謝您的回答,但我結束了使用jQuery的每個方法來代替。這似乎對我很好。 –