JQuery after submit form post link is not send? [duplicate] - javascript

Hello Friends this is my code to form submit and then send post link but form submit success then after not send post link.
document.getElementById("pitch_image_path_form").submit(function(e){
$.post("submit_investorform.php",{'flage':'getallimagesfromselectedid','form':'pitch_image_path_form'},function(result){
$("#pitch_image_path_showalldatafromid").html(result);
});
e.preventDefault();
});
this is my code form is submit but post request is not send.

dear renishkhunt please try this code. this is help fully for me.
$("#pitch_image_path_form").ajaxSubmit({ success: function(){
$.post("submit_investorform.php",{'flage':'getallimagesfromselectedid','form':'pitch_image_path_form'},function(result){
$("#pitch_image_path_showalldatafromid").html(result);
});
} });
please check this link this is tutorial.
http://malsup.com/jquery/form/

.submit() is a jQuery function, so you need to wrap your $("#pitch_image_path_form")[0] in a jQuery wrapper, like so:
$($("#pitch_image_path_form")[0]).submit(function(){

There is no submit() event in DOM, you are mixing DOM and jQuery
change
document.getElementById("pitch_image_path_form").submit
to
$("#pitch_image_path_form").submit

$("pitch_image_path_form").submit(function(e){
e.preventDefault();
$.post(
"submit_investorform.php",
{'flage':'getallimagesfromselectedid','form':'pitch_image_path_form'}
, function(result) { $("#pitch_image_path_showalldatafromid").html(result); }
);
});

Because DOM does not support submit(), there is do submit() function in DOM. Same exact answer when you asked this the last time.

When you call .submit(), it posts the form with the specified action of the <form>.
You might want to stop the propagation, by using either event.stopPropagation();, event.preventDefault(); or return false; at the end of your function.
$("#pitch_image_path_form").submit(function(event){
event.stopPropagation();
event.preventDefault();
//Your .post()
return false;
});
Plus, as epascarello pointed out, .submit() is a jQuery function.
If you want to use it, use it on the jQuery object by removing [0] before submit(), as you're not supposed to have multiple elements with the same ID.

Related

jQuery event.Preventdefault(); is not doing anything

i have some scripts on my site that send replies to a server and reload the page using ajax. sometimes though, it reloads the page for no reason.
so I added this code
<script type="text/javascript">
$("form").submit(function(){
event.preventDefault();
return false;
)
</script>
but it is still reloading the page when i hit sumbit.
why.
You simply forgot to bring in the event argument into the handler:
$("form").submit(function( event ) {
Also: check the error console, in your example code you are missing an ending bracket });
As a side note: make sure you are selecting the form element after it has been loaded, to be safe use domReady:
$(function() {
$("form").submit(function(event){
event.preventDefault();
});
});
event as a global (window.event) is non-standard. jQuery normalizes this for you, so just pass in event as an argument:
$(document).ready(function() { // Make sure you wait until the document is ready
$("form").submit(function(event) {
event.preventDefault();
});
});
Also, returning false calls event.preventDefault() as well as event.stopPropagation(). You only want the first to happen, so be explicit.
$("form").submit(function(event){})
In a comment on another answer, you've said:
it still reloads the page
That suggests that you have this code above the form you want it to act on in the HTML. Move the script below the form (usually just before the closing </body> tag is best), or use jQuery's ready event.
Also note that if you're using return false, you don't need event.preventDefault, because return false from a jQuery event handler does event.preventDefault() and event.stopPropagation(), so:
<form ...>
<!-- ... -->
</form>
<!-- ... -->
<script>
$("form").submit(function(){
return false;
});
</script>
</body>
Or alternately, this anywhere on the page, using the shortcut for ready:
$(function() {
$("form").submit(function(){
return false;
});
});

Delegate to capture two form submissions

Using the following function to capture two dynamically generated form submissions, but is not preventing their submission.
$('body').delegate('.edit_faq, .new_faq', 'submit', function(e){
e.preventDefault();
//voodoo
});
I was using the following, using live. This works, but live is depreciated and the end result is for this code to end up in a flexible plugin.
$('.edit_faq').add('.new_faq').live('submit', function(e){
e.preventDefault();
//magic
});
What you have written is actually working.
check:
http://jsfiddle.net/peuqU/
press run as jsfiddle sometimes needs a refresh before testing submits.
I have only been able to test it with chrome 23.
Try on which is the recommended way, and return false:
$('body').on('submit', '.edit_faq, .new_faq', function(e){
e.preventDefault();
//Do your stuff here.
});
As to why the submission still occurs, maybe you have a JS error somewhere in the page?
FIXED--
The problem was that the code required being wrapped on a document onReady:
$(document).ready(function(){
$('body').on('submit', '.edit_faq, .new_faq', function(e){
e.preventDefault();
//voodoo
});
})
I occurs to me that since live did not require the ready, that maybe this is still wrong?

jQuery preventDefault function not working on jsfiddle

I cannot get the following simple jQuery to work. I know I am overlooking something, so please help me out. Here is the code at http://jsfiddle.net/Z5waA/. It's a simple click the button, then alert with jQuery.
preventDefault(e); should be e.preventDefault();
code should be like this,
$('#submitResetPass').bind('click', function(e) {
e.preventDefault();
alert("hello");
});
and you need to add jQuery reference.
You had a couple issues. First, the jsFiddle wasn't set for jQuery. Then, your call to preventDefault wasn't correct. It works here: http://jsfiddle.net/jfriend00/v9aVb/.
$('#submitResetPass').bind('click', function(e) {
e.preventDefault();
alert("hello");
});
Use e.preventDefault() instead of preventDefault(e).

JS: Posting form directly

If on document.ready, I want my form to auto post itself, is this possible?
So when the document has loaded, then it posts itself and takes to the action="" (in this case is ?upload=true).
I heard about $.post() jquery, but this is just for posting in the background?
$.post() is an AJAX call and will not force the entire page to postback.
you may want to try
$(document).ready(function(){
var form = $("#yourformid");
form.submit();
});
Yes, $.post() in jQuery is for POST requests with AJAX.
If you want a form to submit on the document ready event, just invoke the submit itself!
$(document).ready( function()
{
$('form').submit()
});
You want something like this:
$(function() {
$('#myformid').submit();
});
See the documentation for jQuery's submit().
And see this working demo: http://jsfiddle.net/8hg8s/

Add attributes to textbox using jQuery

I am using asp.net MVC.
I have control like
<%= Html.TextBox("username") %>
I want lost focus event to that control.
So code like
$(document).ready(function() {
$("#username").Attributes.Add("onblur", "alert('losing focus');");
});
but it is not working,
Ultimate goal is to check password & confirm password matches
help me!
It looks like you're trying to use C# code in jQuery?
The easiest way to bind an event to onblur in jQuery is:
$("#username").blur(function() { alert('losing focus'); });
More information on blur() is available at http://docs.jquery.com/Events/blur
You can try attach to this event with another way, like this:
$("#username").bind("blur", function(e){
alert('hello');
});
$(document).ready(function() {
$("#username").blur(function() {
alert('byebye focus');
});
});
http://docs.jquery.com/Events/blur
I believe your jQuery syntax is wrong,
You want to bind an event, "onBlur" that fires the alert, so try
$("#username").blur(function(){
alert("loosing focus");
});
-- update, looks like some one answered this as I was answering

Categories

Resources