How to submit form, if input out of shape? - javascript

Please tell me how to make it work in older browsers as html5 not understand everything, but inside the form can not be put input:
<form onsubmit="checkd();return false" id="form2">
...
</form>
<input type="submit" value="Submit" form="form2" />
P.S. I do not know JS, be grateful for a detailed explanation or example ^^

With JQuery you may do something like:
$("form2").on("submit", function(){
...
});

Related

onkeypress is not working in form

I have the following problem with this code:
<form>
<div class="searchBarDiv"> <input class="searchBar" id="search" type="search"
placeholder="Suche" onkeypress="search();"> </div>
</form>
search(); won't get fired if i press a key. But when i remove the <form></form> everything works fine.
Is there any way to fire onkeypress in a form or any other function?
I`m using PhoneGap and jQuery. It won't work in a Browser or on a mobile Device.
Any help would be nice. Thank you in advance!
Edit:
Solved by A. Wolff: You can't use the id 'search' in a form. Thank you all for you answers and solutions.
I would suggest something more like this. Remove the onkeypress from the input, and as per #A.Wolff's comment on the question, change the search input's ID. Then add the following JS on document ready.
$("#search_inp").keypress(search);
Make sure that the search function takes an event parameter if you need to know which key was pressed.
Try to change your js function name or input id , your problem is because id and function name are same .
<form>
<div class="searchBarDiv"> <input class="searchBar" id="search" type="search"
placeholder="Suche" onkeypress="searchData();"> </div>
</form>
<script type="text/javascript">
function searchData(){
alert("2222");
}
</script>

Submiting a form from javascript (using jQuery) opens in new tab in Opera 12.16

I have a huge and annoying problem.
I'm using jQuery (version 1.7.1, it's not an option to update jQuery or the browser).
I added some Javascript to handle <ctrl>+s as a submit, instead of a save.
It works perfectly well in every browser, except Opera.
In Opera, it submits, but to a new page.
If anyone has found a way to fix this, can you please provide some help? I would appreciate a lot!
This is quite an annoying bug and I use that feature a lot in the backoffices I'm developing.
Here is an example of the Javascript code used:
$(window).keydown(function(e){
if(e.ctrlKey&&e.keyCode==83)
{
e.preventDefault();//disable page saving
var a=$(':focus');//gets the focused element
if(!a.length){a=$('form:eq(0)');}else{a=a.parents('form');}//if no element is focused, gets the 1st form
a.find("input[type=button].submit, button[type=submit], input[type=submit], form a.submit").click();//triggers the click in the button
if(a.find('input[type=submit]'))a.submit();//'silly' line added as a desperate attempt, ignore it
}
});
The HTML code is a simple form like this:
<form action="#" method="POST" [target="_self"]>
<input type="text" name="fld">
<input type="submit" name="sub" value="Submit">
</form>
Using a <button type="submit"> submits to the same page AND to a new tab.
I found a solution!
This is a really UGLY hack.
This is the new Javascript:
$(window).keydown(function(e){
if(e.ctrlKey&&e.keyCode==83)
{
e.preventDefault();
var a=$(':focus');
if(!a.length){a=$('form:eq(0)');}else{a=a.parents('form');}
a.find("input[type=button].submit, form a.submit").click();
if(window.opera&&a.find('input[type=submit],button[type=submit]').length)a.submit();
else a.find("input[type=submit],button[type=submit]").click();
}
});
And the HTML must change to this:
<form action="#" method="POST" [target="_self"]>
<input type="text" name="fld">
<input type="hidden" name="sub" value="1"><!-- to validate if the form was submitted on opera -->
<input type="submit" name="sub" value="Submit">
</form>
Since the form submission works perfectly on the other browsers, we can leave the default behavior for them, changing only the way that Opera works.
As I said: it's an UGLY hack, but works.
Thank you #Bergi for your idea. I just had to adapt it.

submit button does nothing

I have the following code:
<button data-dojo-type="dijit.form.Button" data-dojo-props='baseClass:"styleButton"'
name="_action_update"
type="submit"
label="Save"
>Save</button>
The problem is, that this doesnt seem to actually work as a submit action anymore. If I take away the dojo related stuff, it works as expected. I have used this baseClass method before to apply a style to a button, but not a submit button. How should I change this?
I think you need an input type="submit"
<input data-dojo-type="dijit.form.Button" data-dojo-props='baseClass:"styleButton"'
name="_action_update" type="submit" value="Save" />
Or if you really need a button tag, something ugly like this should work
<button onclick="document.getElementById('yourFormId').submit()" data-dojo-type="dijit.form.Button" data-dojo-props='baseClass:"styleButton"'
name="_action_update" label="Save">Save</button>
Or of course you could more elegantly attach the event handler without the dom level-0 cruft
<button id="formSubmitBtn" data-dojo-type="dijit.form.Button" data-dojo-props='baseClass:"styleButton"'
name="_action_update" label="Save">Save</button>
document.getElementById("formSubmitBtn").onclick = function() {
document.getElementById('yourFormId').submit();
};
You have not precised what version of Dojo you are using, but, and I believe that is the problem :
Dojo version < 1.7 do not support data-dojo-props + natural html properties
<input data-dojo-type="dijit.form.Button" data-dojo-props='baseClass:"styleButton"'
name="_action_update" type="submit" value="Save" />
Will not work, name, type and value will be ignored.
If your dojo version is 1.7 or 1.7.1, then the problem is elsewhere.
Try adding a data-dojo-type="dijit.form.Form to you form tag as such
<div dojoType="dijit.form.Form" id="myFormThree" jsId="myFormThree" encType="multipart/form-data"
action="" method="">
referrenced from: http://dojotoolkit.org/reference-guide/dijit/form/Form.html
here is the docs for dijit.form.Button, not sure how helpful they will be.
http://dojotoolkit.org/reference-guide/dijit/form/Button.html

Javascript injection

I would like to fill out and a submit a form explicitly with JavaScript. First, I thought I had to use window.open but it's certainly wrong because if it gets loaded, the left of my scripts written for example in a html file would be ignored.
Do I have to create a .js file and fire that one?
uhhhh...not exactly sure how this relates to injections...you can do this with jQuery in a handful of lines of code.
say you have the following form:
<form id="theForm" name="testForm" action="whatever.php" method="get">
<input type="text" name="cow" />
<input type="text" name="sheep" />
<input type="text" name="pig" />
<input type="submit" value="Submit" />
</form>
If you have jQuery loaded, all you need to do is this:
function submitForm(){
var cowVal="Cows go moooo!";
var sheepVal="Sheep go baaaaaaah!";
var pigVal="pigs go sooooeeeeeeeh!";
$("#theForm input").eq(0).val(cowVal).next().val(sheepVal).next().val(pigVal).parent().submit();
}
Hope that helps!

Why doesn't this JavaScript work in IE8?

<form action="http://google.com">
<input type="submit" onclick="javascript:this.disabled='disabled';return true">
</form>
It works in Firefox, but not in IE8, Safari (It should forward me to Google's site, it but doesn't happen.). Why?
Try this instead:
<input type="submit" onclick="this.disabled='disabled';form.submit()">
Edit: Added form. as per wamp's comment.
You don't need the javascript: pseudoprotocol. This should work:
onclick="this.disabled=true"
and you don't need to return true, that'll happen automatically.

Categories

Resources