简体   繁体   中英

Unable to call a function with ENTER key

I need to pass in some values to my function I want the function to run when I press ENTER KEY in PHP

the function is update() This is my PHP

    echo '<input type="text" size="23" id= n'.$row["ContactID"].'
class = "name" placeholder="Contact Name" 
value="'.$row["Name"].' 
onkeydown="if (event.keyCode == 13) updatename(this,'.$row["ContactID"].')  ">';

my javascript

function updatename(item, cid)
{
            var varname =  $(item).val();   
            var originaltext =  $(item).val();  
            $.ajax({
                url: 'changeContact.php',
                type: 'POST',
                data:{
                varname: varname
                },
                success:function (data) {

                    if (data == '1')
                    {
                        $("#status")
                        .addClass("success")
                        .html("Data saved successfully")
                        .fadeIn('fast')
                        .delay(3000)
                        .fadeOut('slow');   

                    }
                    else
                    {
                        $("#status")
                        .addClass("error")
                        .html("An error occured, the data could not be saved")
                        .fadeIn('fast')
                        .delay(3000)
                        .fadeOut('slow');   
                    }
                }
            });   


         }

Why does it don't seem to be working? Nothing have been send to my database, even I change to a simple alert, nothing appear How can I improve it?

<input type="text" id="your id" onkeypress="MAGIC(event);" />

function MAGIC(event){
  $("#id").keyup(function(event){
        if(event.keyCode == 13){
            //your code to be executed
        }
   }

Probably because your HTML is invalid.

...
value="'.$row["Name"].'                 <--------- " is missing
onkeydown="if (eve...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM