开发者

How do I submit form using ajax (jquery) with text anchor?

I'm trying to submit a form with jquery ajax plugin (jquery.form.js) and text anchor. But, before the form get submitted, I need a confirmation from the user first.

Here's the javascript:

<script type="text/javascript" src="../scripts/jquery.js"></script>
<script type="text/javascript" src="../scripts/jquery.form.js"></script>
<script>
   function confirmDelete(speciesID, species) {         
        var answer = confirm('Are you sure you want to delete ' + species + '?');
        if (answer == true)
        {
            var options = { 
                target:        '#deleteSpeciesOutput'开发者_Go百科 + speciesID
            };      
            $('#deleteSpeciesForm' + speciesID).ajaxForm(options); 
        }
        return false;
    }
</script>

HTML in PHP:

<form id=\"deleteSpeciesForm$speciesID\" action=\"processDeleteSpecies.php\" method=\"post\">
    <input type=\"hidden\" name=\"species\" value=\"" . $species[0] . "\" />
            <a href=\"#\" onclick=\"return confirmDelete('deleteSpeciesForm$speciesID', '" . $species[0] . "');\">delete</a>
</form>
<div id=\"deleteSpeciesOutput$speciesID\"></div>

The confirmation dialog shows up, but nothing happens after I click 'Yes'. Thanks for your help.


Don't use onclick in your HTML. Why would you when you are already using jQuery?

HTML

<form id="deleteSpeciesForm$speciesID" action="processDeleteSpecies.php" method="post">
  <input type="hidden" name="species" value="species">
  <a href="#">delete</a>
</form>

JS

$(document).ready(function() {
  $('#deleteSpeciesForm$speciesID a').click(function() {
    var form = $(this).parent();
    var species = $('input[name=species]', form).val();

    var answer = confirm('Are you sure you want to delete ' + species + '?');
    if (answer) {
      form.submit();
    }

    return false;
  });
});


Try having it return true at the end.

$(document).ready(function() {
  $('#deleteSpeciesForm$speciesID a').click(function() {
    var form = $(this).parent();
    var species = $('input[name=species]', form).val();

    var answer = confirm('Are you sure you want to delete ' + species + '?');
    if (answer) {
      form.submit();
    }

    return TRUE;
  });
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜