Featured post
jQuery not calling form .submit() when using Ajax on IE -
i using jquery plugin magic during .submit
of form (creates hidden string , writes <input type="hidden">
item). on ie works if submit form via post, if use ajax, .submit
callback never called. here example of flow:
<script type="text/javascript"> (function (jquery) { jquery.fn.createhidden = function () { // 1. want have called $(this).submit(function () { var hiddentext = $(document.createelement('input')); hiddentext.attr('name', 'hidden'); hiddentext.attr('type', 'hidden'); hiddentext.val('1'); $(this).append(hiddentext); alert("1"); }); // 2. came workaround $(this).parents('div.formwrapper').submit(function () { var form = this.children('form'); if (form && form.tagname == "form") { var hiddentext = $(document.createelement('input')); hiddentext.attr('name', 'hidden'); hiddentext.attr('type', 'hidden'); hiddentext.val('2'); $(form).append(hiddentext); alert("2"); } }); }; })(jquery); </script> <script type="text/javascript"> $(document).ready(function () { $(".form").createhidden(); }); </script> <div class="formwrapper"> @*using (html.beginform("about", "home", formmethod.post, new { @class = "form", @id = "form" }))*@ @using (ajax.beginform("about", "home", new ajaxoptions { updatetargetid = "myreply", httpmethod = "post" }, new { @class = "form", @id = "form" })) { <div class="exposeddata"> @html.textbox("data", "", new { @class = "data" }) <input type="submit" value="button" /> <span id="myreply"></span> </div> } </div>
// mvc homecontroller.cs public class homecontroller : controller { public actionresult index() { return view(); } public string about(string hidden) { return hidden; } }
when using http post on ie, see alerts 1 & 2, when using ajax see alert 2. firefox shows 1. chrome, safari , opera show 1 & 2.
is there better way of fixing works on browsers? related jquery.unobtrusive-ajax.js
i'm using in mvc3?
i've tried $('form').ajaxstart()
, ajaxoptions{onbegin=...}
, <form onsubmit=...>
didn't trick called after controller visited, , hence hiddentext not included in form.
you should try jquery froms plugin - works in browsers.
and maybe rid of idea form has submit, ajax requires no forms.
hidden inputs inside forms not recommend, can manipulated.
edit:
including plug-in plug-in not best way...
i'd rather recommend use native $.ajax function, here's real-world example wrote:
$('li.sheet').unbind().bind('click', function() { $.id = $(this).attr('id'); if($(this).next('div.edit').length == 0){ $.ajax({ url: 'ajax_handler.php?mode=form&sheet='+$.id, success:function(r){ $('li#'+$.id).after(r); $('li#'+$.id).next('div.edit').toggle(200); } }); } else { $(this).next('div.edit').toggle(200, function(){ $(this).remove(); }); } });
- Get link
- X
- Other Apps
Comments
Post a Comment