如何使用带有文本锚点的ajax(jquery)提交表单?

时间:2011-04-28 23:18:27

标签: jquery ajax

我正在尝试使用jquery ajax插件(jquery.form.js)和文本锚提交表单。但是,在表单提交之前,我需要首先得到用户的确认。

这是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' + speciesID
            };      
            $('#deleteSpeciesForm' + speciesID).ajaxForm(options); 
        }
        return false;
    }
</script>

PHP中的HTML:

<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>

显示确认对话框,但点击“是”后没有任何反应。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

请勿在HTML中使用onclick。当你已经在使用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;
  });
});

答案 1 :(得分:0)

尝试在最后返回true。

$(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;
  });
});