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

时间:2022-11-24 00:11:41

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.

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

Here's the javascript:

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

HTML in PHP:

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>

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

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

2 个解决方案

#1


0  

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

不要在HTML中使用onclick。当你已经在使用jQuery时为什么会这样?

HTML

HTML

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

JS

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

#2


0  

Try having it return true at the end.

尝试让它在最后返回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;
  });
});

#1


0  

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

不要在HTML中使用onclick。当你已经在使用jQuery时为什么会这样?

HTML

HTML

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

JS

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

#2


0  

Try having it return true at the end.

尝试让它在最后返回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;
  });
});