将变量从 onclick 传递到 ajax


Passing a variable from onclick to ajax

我正在尝试将变量(区域)从onclick元素传递到ajax函数。我正在使用PHP,SQL和插件 http://t4t5.github.io/sweetalert/

这是我在索引头中的代码.php

   <script type"text/javascript">
  function save(){
      $.ajax({
          type: "POST",
          url: "addlandtovisited.php",
          data: {region: region},
          success: function(data) {
              alert("Ajax save executed!");
          }
      });
  }
</script>
<script>
  jQuery(document).ready(function () {
    jQuery('#vmap').vectorMap({
      map: 'world_en',
      backgroundColor: '#333333',
      color: '#ffffff',
      hoverOpacity: 0.7,
      selectedColor: '#666666',
      enableZoom: true,
      showTooltip: true,
      scaleColors: ['#C8EEFF', '#006491'],
      values: sample_data,
      normalizeFunction: 'polynomial',
      onRegionClick: function (element, code, region) {
        var boton = "button";
        swal({   
              title: ''+region,  
              showCancelButton: true, 
              showConfirmButton: false, 
              text: '<a href="" onclick="save(region)">test</a>',
              html: true 
        });
      }
    });
  });
</script>

阿德兰托访问.php:

<?php
if(isset($_POST['region'])){ 
?>

当我将字符串设置为 ajax 函数并从 save(region) 中删除区域时,它工作正常:

data: {region: "TEST"},
text: '<a href="" onclick="save()">test</a>',

正如这个线程所暗示的那样,甜味警报存储库存在一些问题。例如,我无法使 swal 在正文中呈现 html 文本。切换到 sweetalert2 并应用线程提到的补丁可能是个好主意。

我建议您使用确认按钮,而不是在swal体内创建自己的链接。例如:

swal({
  title: '' + region,
  showCancelButton: true,
  showConfirmButton: true,
  confirmButtonText: "save",
  text: "anything",
  html: true
}, function() { // save button callback
  save(region);
});

不要忘记将 region 参数添加到您的save()方法中:save(region)