Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 单击或按enter键运行函数?_Jquery_Ajax - Fatal编程技术网

Jquery 单击或按enter键运行函数?

Jquery 单击或按enter键运行函数?,jquery,ajax,Jquery,Ajax,我有以下ajax和jquery代码,当用户单击表单上的submit按钮时运行这些代码。但是,我还想在用户按下enter键时运行代码?有人能告诉我怎么做吗?提前谢谢 我已经试过了,但如果我这样做,它似乎根本不起作用: $(document).on('keypress click', '#submit', function() { 代码: <script type="text/javascript"> $(document).ready(function() { $(document

我有以下ajax和jquery代码,当用户单击表单上的submit按钮时运行这些代码。但是,我还想在用户按下enter键时运行代码?有人能告诉我怎么做吗?提前谢谢

我已经试过了,但如果我这样做,它似乎根本不起作用:

$(document).on('keypress click', '#submit', function() {
代码:

<script type="text/javascript"> 
$(document).ready(function() {
$(document).on('keypress click', '#submit', function() {
var myusername = $("#myusername").val();
var mypassword = $("#mypassword").val();

if (myusername == null || myusername == "" || mypassword == null || mypassword == "") {
if (myusername == null || myusername == "") { document.forms["form"]["myusername"].style.border = "2px solid #963634";}
if (mypassword == null || mypassword == "") { document.forms["form"]["mypassword"].style.border = "2px solid #963634";}
$( ".home_column" ).effect( "shake" ); 
} else {
// Returns successful data submission message when the entered information is stored in database.
$.post("include/validate_login.php", {
username1: myusername,
password1: mypassword
}, function(data) {
if (data.indexOf("login_wrong") >= 0){
$(".home_column").flip({
    direction:'lr',
        color: 'rgba(138, 138, 138, 0.2)', 
            content:'<h21>Incorrect Login Details</h21>'
})  
setTimeout(
  function() 
  {
   $(".home_column").revertFlip()
  }, 2500);  }else{
 if (data.indexOf("login_success") >= 0){
  $(".home_column").flip({
    direction:'lr',
        color: 'rgba(138, 138, 138, 0.2)', 
        content: '<h21>Hello '+data.substring(13)+'</h21><br/><br/><h21>Please Wait...</h21>'    
})  
setTimeout(
  function() 
  {
   window.location = 'dashboard.php'; 
  }, 2500);

  } }
$('#form')[0].reset(); // To reset form fields
});
}
});
});

</script>

$(文档).ready(函数(){
$(文档)。在('按键单击','提交',函数()上){
var myusername=$(“#myusername”).val();
var mypassword=$(“#mypassword”).val();
如果(myusername==null | | myusername==“”| | mypassword==null | | mypassword==“”){
如果(myusername==null | | myusername==“”){document.forms[“form”][“myusername”].style.border=“2px solid#963634”}
如果(mypassword==null | | mypassword==“”){document.forms[“form”][“mypassword”].style.border=“2px solid#963634”}
$(“.home_列”)。效果(“震动”);
}否则{
//当输入的信息存储在数据库中时,返回成功的数据提交消息。
$.post(“include/validate_login.php”{
username1:myusername,
密码1:mypassword
},函数(数据){
if(data.indexOf(“登录错误”)>=0){
$(“.home\u列”).flip({
方向:'lr',
颜色:“rgba(138,138,138,0.2)”,
内容:'不正确的登录详细信息'
})  
设置超时(
函数()
{
$(“.home_列”).revertFlip()
},2500);}其他{
if(data.indexOf(“登录成功”)>=0){
$(“.home\u列”).flip({
方向:'lr',
颜色:“rgba(138,138,138,0.2)”,
内容:“你好”+数据。子字符串(13)+'

请稍候…” }) 设置超时( 函数() { window.location='dashboard.php'; }, 2500); } } $('#form')[0].reset();//重置表单字段的步骤 }); } }); });
您需要在按键功能中执行此操作,然后您可以检查是否按了“enter”(13)并提交表单

function submitForm() {
    //Form submission functionality goes here
}

$("input").keypress(function(e) {
    if (e.which === 13) {
        e.preventDefault();
        submitForm();
    }
});

$("#formID").submit(function(e) {
    e.preventDefault();
    submitForm();
});

只需将要运行的代码放在函数中,然后为click事件和keypress事件调用该函数(如APAD1所述)


但这只是一个解决方案,当用户点击键盘上的enter键时运行代码,但我需要在用户点击submit或enter键时运行代码?此解决方案仅允许在点击enter时运行代码。您说过,当用户点击submit时,代码已经可以运行了。是的,我可以,但是如果取出$(文档)。在('click','#submit',function(){上运行脚本并用$('input')替换。按键(function(e){那么脚本只有在我点击enter时才会运行,所以即使用户点击submit,脚本也不会运行,它只会在他们点击enter时运行。但是我需要在用户点击submit或点击enter@Apad1时运行此脚本。您不应该替换任何内容,上面的代码应该在您已有代码的基础上使用。我不想以wi结束对于这两个版本的脚本,我宁愿合并on-click和on-enter函数来执行我的一个脚本谢谢你的代码,但是我尝试了这个,它似乎根本没有执行代码
// bind UI actions
$(document).on('click', '#submit', myFunc);
$("input").keypress(function(e) {
    if (e.which === 13) {
        myFunc(); 
    }
});

// functions
function myFunc() {
    // place your code here
}