开发者

change prototype script to jquery one

开发者 https://www.devze.com 2022-12-24 23:16 出处:网络
I have this form submit code: Event.observe(window, \'load\', init, false); function init() { Event.observe(\'addressForm\', \'submit\', storeAddress);

I have this form submit code:

Event.observe(window, 'load', init, false);
function init() {
  Event.observe('addressForm', 'submit', storeAddress);
}
function storeAddress(e) {
  $('response').innerHTML = 'Adding email address...';
  var pars = 'address=' + escape($F('address'));
  var myAjax = new Ajax.Updater('response', 'ajaxServer.php', {method: 'get', parameters: pars});
  Event.stop(e);
}

How can I开发者_运维问答 change it to work with jQuery?

Here is the html form:

<form id="addressForm" action="index.php" method="get">
  <b>Email:</b> <input type="text"  name="address" id="address" size="25"><br>
  <input name="Submit" value="Submit" type="submit" />
  <p id="response"><?php echo(storeAddress()); ?></p>
</form>

and this is php code at start of document:

<?php
require_once("inc/storeAddress.php");
?>


Something like this:

$(function() {
    $('#addressForm').submit(function() {
        $('#response')
            .html('Adding email address...')
            .load('ajaxServer.php', { address: $('#address').val() }, function() {
                $('#address').val('');
            });
        return false;
    });
});

Note: when using .load(), the POST method is used if data is provided as an object; otherwise, GET is assumed. So, if you want to pass the data as GET, use:

.load('ajaxServer.php', 'address='+$('#address').val());

Or just get the submitted data from $_POST['address'] which would be wiser and easier in my opinion.


You can convert the script like this:

$(function() {
  $('#addressForm').submit(function(e) {
    $('#response').html('Adding email address...');
    $.ajax({
        url: 'ajaxServer.php',
        type: 'GET',
        data: {'address': $('#address').val() },
        success: function(response) {
          $('#response').html(response);
        }
    });
    e.preventDefault();
    return false;
  });​
});

Here's a rundown of the equivalents:

  • .html() for the innerHTML call
  • $('#id') selector for finding by an ID
  • $.ajax for the ajax call
    • data is the passed parameters, set the pair up (address=...)
    • success callback runs when the request completes
      • put the response into the id="response" div here


Give this a whirl..

function init()
{
    $('#addressForm').submit(storeAddress);
}

function storeAddress(e)
{
    $('#response').html('Adding address...');

    var params = 'address=' + $('#address').val();

    $('#response').load('ajaxServer.php', params);

    $.ajax({
        url : 'index.php',
        data : $('#addressForm').serialize(),
        success : function(data)
        {
            alert('yay, form data sent!');
        }
    });

    // Fix - stops page reload
    return false;
}

$(document).ready(init);
0

精彩评论

暂无评论...
验证码 换一张
取 消