2015-04-07 61 views
3

我有一个小问题,我猜在我看来听起来很愚蠢。其实我也该代码javascript button ajax和php

<script type="text/javascript"> 
$(document).ready(function(){ 

var email_value = prompt('Please enter your email address'); 
if(email_value !== null){ 
//post the field with ajax 
$.ajax({ 
    url: '="/new/cfolder.php', 
    type: 'POST', 
    dataType: 'text', 
    data: {data : email_value}, 
    success: function(response){ 
    //do anything with the response 
     console.log(response); 
    }  
}); 
} 

}); 
</script> 

我想将其链接到我的按钮,这是否

<form action="/new/cfolder.php" method="post"> 
    </font><input type="submit" value="Create Vdisk" class="myButton6" onClick="function()"> 
<br></form> 

它实际上是有可能做到这一点?谢谢你的回答。

回答

1
<script type="text/javascript"> 
    $(document).ready(function(){ 
    $(document).on('click', '.myButton6', function(){ 
     var email_value = prompt('Please enter your email address'); 
     //post the field with ajax 
     if(email_value.length > 0){ 
     $.ajax({ 
      url: '/new/cfolder.php', 
      type: 'POST', 
      dataType: 'text', 
      data: {data : email_value}, 
      success: function(response){ 

       alert(response); 
      }  
     }); 
     } 
    )}; 
    }); 
</script> 

尝试这种方式

0
// this is the id of the form 
$("#idForm").submit(function() { 

var url = "path/to/your/script.php"; // the script where you handle the form input. 

$.ajax({ 
     type: "POST", 
     url: '="/new/cfolder.php', 
     data: $("#idForm").serialize(), // serializes the form's elements. 
     success: function(data) 
     { 
      alert(data); // show response from the php script. 
     } 
    }); 

return false; // avoid to execute the actual submit of the form. 
}); 

给你的表格一个id

<form id="idForm" action="/Same/path/as/your/ajax/" method="post"> 
</font><input type="submit" value="Create Vdisk" class="myButton6" onClick="function()"> 
<br></form> 
0
Yes of course you can do this . Like this code and you will have to include jquery1.9.0.js or above. 


<script type="text/javascript"> 
    $(document).ready(function(){ 
    $("#myButton6").click(fuction(){ 
    var email_value = prompt('Please enter your email address'); 
    if(email_value !== null){ 
    //post the field with ajax 
    $.ajax({ 
     url: '/new/cfolder.php', 
     type: 'POST', 
     dataType: 'text', 
     data: {data : email_value}, 
     success: function(response){ 
     //do anything with the response 
      console.log(response); 
     }  
    }); 
    } 

    }); 

    }); 
    </script> 
    I would like to link it to my button which does this 


</font><input type="submit" value="Create Vdisk" class="myButton6" id="myButton6"> 
    <br> 
+0

我做了什么你已经做了,但它只是刷新我的网页。 –