输入密钥提交Ajax表单

问题描述 投票:0回答:7
我有一个非常简单的 AJAX 表单,要求提供电子邮件地址并在提交后将其发送给我。

如何在按下回车键时获取要提交的表单?

当用户单击提交按钮时运行:

<script type="text/javascript"> $(document).ready(function () { $("#submit_btn").click(function () { // Get input field values: var user_email = $('input[name=email]').val(); // Simple validation at client's end // We simply change border color to red if empty field using .css() var proceed = true; if (user_email === "") { $('input[name=email]').css('border-color', 'red'); proceed = false; } // Everything looks good! Proceed... if (proceed) { /* Submit form via AJAX using jQuery. */ } }); // Reset previously set border colors and hide all message on .keyup() $("#contact_form input, #contact_form textarea").keyup(function () { $("#contact_form input, #contact_form textarea").css('border-color', ''); $("#result").slideUp(); }); }); </script>


我知道这个问题之前已经被问过——我无法让

keypress

 功能正常工作。

我试过了,没有效果:

$("#contact_form").keypress(function (e) { if ((e.keyCode == 13) && (e.target.type != "textarea")) { e.preventDefault(); // Get input field values var user_email = $('input[name=email]').val(); // Simple validation at client's end // We simply change border color to red if empty field using .css() var proceed = true; if (user_email === "") { $('input[name=email]').css('border-color', 'red'); proceed = false; } // Everything looks good! Proceed... if (proceed) { /* Submit form via AJAX using jQuery. */ } } });

形式为

#contact_form

如有任何帮助,我们将不胜感激......

javascript php jquery ajax forms
7个回答
12
投票
只要将提交事件绑定到你的表单上,然后回车键也可以工作:

$("#contact_form").submit(function (e) { e.preventDefault(); // Get input field values var user_email = $('input[name=email]').val(); // Simple validation at client's end // We simply change border color to red if empty field using .css() var proceed = true; if (user_email === "") { $('input[name=email]').css('border-color', 'red'); proceed = false; } if (proceed) { // Insert the AJAX here. } });

代码在这里:

http://jsfiddle.net/6TSWk/6/


1
投票
在每个字段框或复选框中添加新类=>类按键按钮 然后用下面的代码替换按键上的代码:

$(document).on("keypress",".keypressbutton",function(event) { var keyCode = event.which || event.keyCode; if (keyCode == 13) { $("#submit_btn").click(); return false; } });
    

1
投票
$("#myform").keypress(function(event){ if(event.keycode===13){ // enter key has code 13 //some ajax code code here //alert("Enter key pressed"); } });
    

0
投票
您的

if

 语句中有两个左括号,但缺少一个右括号。另外,我会改变
e.target.type
。试试这个:

$("#contact_form").keypress(function (e) { if ((e.keyCode == 13) && ($('input[name="email"]').is(':focus'))) { e.preventDefault(); //get input field values var user_email = $('input[name=email]').val(); //simple validation at client's end //we simply change border color to red if empty field using .css() var proceed = true; if (user_email === "") { $('input[name=email]').css('border-color', 'red'); proceed = false; } } });
    

0
投票
您可以使用提交按钮,而不是使用单击按钮。 加载 validate.js 文件

function validateEmail(email) { var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ if (reg.test(email)) { return true; } else{ return false; } } $("#test-form").validate({ submitHandler: function(form) { //form.submit(); var email=$("#email").val(); if(email=='' ) { // Here you can type your own error message $('#valid').css("display","none"); $('#empty').css("display","block"); return false; } if (!(validateEmail(email))) { $('#empty').css("display","none"); $('#valid').css("display","block"); return false; } else { $.ajax({ url: "signup.php", type: form.method, data: $(form).serialize(), success: function(response) { } }); } } }); });
    

0
投票
简单的方法是这样的:

在 HTML 代码中:

<form action="POST" onsubmit="ajax_submit();return false;"> <b>First Name:</b> <input type="text" name="firstname" id="firstname"> <br> <b>Last Name:</b> <input type="text" name="lastname" id="lastname"> <br> <input type="submit" name="send" onclick="ajax_submit();"> </form>

Js代码中:

function ajax_submit() { $.ajax({ url: "submit.php", type: "POST", data: { firstname: $("#firstname").val(), lastname: $("#lastname").val() }, dataType: "JSON", success: function (jsonStr) { // another codes when result is success } }); }
    

0
投票
`dropdown` <?php require_once 'db.php'; if (!empty($_POST['id'])) { $id = $_POST['id']; $sql = "SELECT * FROM district WHERE id = $id"; $query = pg_query($sql); echo'<option value=""> select your option </option>'; if (pg_num_rows($query) > 0) { while ($row = pg_fetch_assoc($query)) { echo '<option value="' . $row['did'] . '">' . $row['dt'] . '</option>'; } } else { echo "<option value=''>No districts found</option>"; } } ?>
    
© www.soinside.com 2019 - 2024. All rights reserved.