javascript - Live data not submitting using MVC 4.5 unobtrusive validation -


we have form user can move items 1 multi-select box another. using mvc 4.5 , jquery validate microsoft's unobtrusive javascript.

the problem is, when submitting form, values within select boxes don't submitted because user doesn't know after moving items have select items submission.

so solution sounds easy: use jquery select items upon submit.

after doing research (thanks stackoverflow community), able discover necessary jquery code intercept submit process. however, problem arises in when code selects items, pre-existing items in select box selected. live (dynamic) items have been moved not selected.

here code first used:

$('#userprofileform').bind('invalid-form.validate',function(){     $(this).find('.selectbox option').each(function (i) {         $(this).attr("selected", "selected");     }); }); 

we discovered using bind doesn't work live should:

$('#userprofileform').live('invalid-form.validate',function(){     $(this).find('.selectbox option').each(function (i) {         $(this).attr("selected", "selected");     }); }); 

however, using jquery 1.9 , live function has been removed. has been replaced this:

$(document).on('invalid-form.validate', 'form', function () {     $(this).find('.selectbox option').each(function (i) {         $(this).attr("selected", "selected");     }); }); 

however, still doesn't work. replaced selection function alert see if works @ all:

$(document).on('invalid-form.validate', 'form', function () {     alert('test'); }); 

an alert not pop up. ideas?

additional info

for wondering why i'm referencing invalid-form.validate because testing invalid form data. same scenario apply if valid form data. haven't gotten point on how bind live data valid form submission process either.

after inspecting elements, noticed jquery adding selected attribute options. however, options weren't highlighted. had me thinking maybe jquery call add selected attribute wrong.

after experimentation, discovered wrong. appears if use attr function of jquery, doesn't set option selected in dom. using prop function does.

here code:

$(document).on('submit', 'form', function (e) {     $(this).find('.selectbox option').each(function (i) {         $(this).prop("selected", true);     }); }); 

this allows form select options selected upon form submission whether submit handler valid or not. if want select based on invalid info, use this:

$('form').bind('invalid-form.validate', function () {     $(this).find('.selectbox option').each(function (i) {          $(this).prop("selected", true);     }); }); 

enjoy!


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -