Tell me, pliz, how to write JavaScript code so that all absolutely forms on the page are sent with a key combination, besides clicking a button with a mouse.

All we have is the presence of the <form> tag on the page.

Ps. CMS - InstantCMS

Added by

When pressed, the function is called:

 function sendMessage(){ var to_id = $('select#to_id').val(); var to_all = $('input[name=massmail]:checked').length; var to_group = $('input[name=send_to_group]').val(); if (to_id > 0 || to_all == 1){ if (to_all==1 || to_group==1){ to_id = 1; } var action = '/users/'+to_id+'/sendmessage.html'; $('form#newmessage').attr('action', action).trigger("submit"); } else { $('input#gosend').attr('disabled', 'disabled'); } } 

Onclick Submission. This is where personal messages are sent. But on the comments, this function is not called, but still the page reloads.

Updated

In fact, the problem with the reload was that the action attribute in all forms is empty. That is, before submitting, he needs to set an action-URL. In this case, everything works.

    2 answers 2

     <form action="aaa.php" method="post"><textarea class="ctrlSubmit"></textarea></form> <form action="bbb.php" method="get"><textarea class="ctrlSubmit"></textarea></form> <script> var t = document.getElementsByTagName('textarea'); var i = 0;while(t[i]){ if(/ctrlSubmit/.test(t[i].className)){ t[i].onkeyup = function(e){ e = window.event || e; if(e.keyCode == 13 && e.ctrlKey){ alert(this.value); //ну или this.form.submit(); } } } ++i; } </script> 

    The script works without jQuery for all textarea.ctrlSubmit.

    Added by
    Option if only form is known. You can move the event from document to all forms (or make it onsubmit = function(){return false;} ).

     <form action="aaa.php" method="post"><textarea></textarea><input type="text"></form> <form action="bbb.php" method="get"><textarea></textarea></form> <script> function addEvent(elem, type, handler){ if (elem.addEventListener){ elem.addEventListener(type, handler, false); } else { elem.attachEvent("on"+type, function() { handler.call(elem) }); } } addEvent(document, 'keyup', function(e){ e = window.event || e; var t = e.target || e.srcElement; if(t.nodeName.toLowerCase() == 'input' || t.nodeName.toLowerCase() == 'textarea'){ // если мы в инпуте или текстовом поле if(e.keyCode == 13 && e.ctrlKey){ // если нажато ctrl + enter if(t.form){ // если элемент в форме alert(t.value); //ну или t.form.submit(); if(e.preventDefault) e.preventDefault(); else e.returnValue = false; } } } }); </script> 
    • Great you wrote, universally. Using your code, I simplified my own to three lines. - ivkremer
    • However, I wrote in the condition that except for the presence of the & lt; form & gt; on the page we don `t know anything. - Fucking Babai
    • @ling thanks for the detailed and beautiful code, but why does it just reload the page, rather than submit the form? all the options that are yours, that @Kremchik -a work identically, that is, just reload the page. By the way, it should be: t.form.submit (); - Yoharny Babay
    • Once again updated. - ling
    • one
      To put it mildly, this is strange. Chrome, IE7, Safari - works. FF is buggy, but this is solved by resetting the onsubmit form. You do not connect anything more than this script? - ling

    Besides form is there also a textarea ? What is the text typed. We do it onKeyDown .

     <?php if ($_POST["demotextarea"]) { echo htmlspecialchars($_POST["demotextarea"]), "<br />\n"; } else { echo "Введите что-нибудь:<br />\n"; } ?> <head> <script src="http://yandex.st/jquery/1.6.2/jquery.min.js"></script> </head> <form action="<?php echo $_SERVER["PHP_SELF"] ?>"> <textarea name="demotextarea"></textarea> </form> <script> $(document).ready(function(){ $("textarea").keydown(function(e) { if(e.ctrlKey && e.keyCode == 13) { this.form.setAttribute("method", "post"); this.form.submit(); } }); }); </script> 

    Option without jQuery (changed this code after the ling answer, in my opinion it became even easier):

     <form action="/index.php" method="post"> <textarea onkeydown="checkKey(event, this.form)"></textarea> </form> <script> function checkKey(e, form) { if (e.ctrlKey && e.keyCode == 13) form.submit(); } </script> 
    • Well, there is no JQ , but I will try to connect, although I would not like to connect it just for the sake of it. How to be without JQ ? - Yoharny Babay
    • If there are no conflicts with other tools, then there is no sense in connecting. And so I added the answer without jQuery. - ivkremer
    • JQ turns out to be connected to a page (this is a CMS). But for some reason the form is not submitted, but the page is simply reloading. Why is that? here is my code. $ (document) .ready (function () {$ ("textarea"). keydown (function (e) {if (e.ctrlKey && (e.keyCode == 0xA || e.keyCode == 0xD)) {this .form.submit ();}});}); - Yoharny Babay
    • Are you expecting POST? By default, a form can give a GET. - ivkremer
    • one
      I changed the first part of the answer to the full page, it works. - ivkremer