Javascript 将变量从onclick传递到ajax

Javascript 将变量从onclick传递到ajax,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图将变量(区域)从onclick元素传递到ajax函数。 我正在使用PHP、SQL和插件 下面是我在index.php文件头中的代码 <script type"text/javascript"> function save(){ $.ajax({ type: "POST", url: "addlandtovisited.php", data: {region: region},

我试图将变量(区域)从onclick元素传递到ajax函数。 我正在使用PHP、SQL和插件

下面是我在index.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>

函数save(){
$.ajax({
类型:“POST”,
url:“addlandtovisited.php”,
数据:{region:region},
成功:功能(数据){
警报(“已执行Ajax保存!”);
}
});
}
jQuery(文档).ready(函数(){
jQuery(“#vmap”).vectorMap({
地图:“世界”,
背景颜色:“#333333”,
颜色:“#ffffff”,
悬停不透明度:0.7,
所选颜色:“#666666”,
enableZoom:true,
showTooltip:true,
标度颜色:['#C8EEFF','#006491'],
值:样本数据,
正规化函数:“多项式”,
onRegionClick:函数(元素、代码、区域){
var boton=“按钮”;
swal({
标题:''+区域,
showCancelButton:true,
showconfixton:false,
文本:“”,
html:对
});
}
});
});
addlandtovisited.php文件:

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

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

data: {region: "TEST"},

text: '<a href="" onclick="save()">test</a>',
数据:{region:“TEST”},
文本:“”,
正如建议的那样,sweetalert回购协议存在一些问题。例如,我无法在正文中生成swal呈现html文本。切换到并应用线程提到的补丁可能是个好主意

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

swal({
  title: '' + region,
  showCancelButton: true,
  showConfirmButton: true,
  confirmButtonText: "save",
  text: "anything",
  html: true

}, function() { // save button callback
  save(region);
});
不要忘记将
region
参数添加到
save()
方法:
save(region)

正如建议的那样,sweetalert回购存在一些问题。例如,我无法在正文中生成swal呈现html文本。切换到并应用线程提到的补丁可能是个好主意

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

swal({
  title: '' + region,
  showCancelButton: true,
  showConfirmButton: true,
  confirmButtonText: "save",
  text: "anything",
  html: true

}, function() { // save button callback
  save(region);
});

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

为什么函数save不带区域参数?顺便说一句,你应该使用非结构化的js方法绑定事件,而不是在HTML标记中设置onclick属性为什么函数save不带区域参数,您应该使用非结构化的js方法绑定事件,而不是在HTML markupThx中为您的答案设置onclick属性。我之所以在onclick中使用a标签,是因为我需要在弹出窗口中使用两个链接/按钮。sweetalert不支持这一点。无论如何我升级到v2并完成了您的更改。现在我得到一个错误,区域没有定义(uncaughtreferenceerror:region-notdefinedsave),给我一个JSFIDLE,我会看看你的答案。我之所以在onclick中使用a标签,是因为我需要在弹出窗口中使用两个链接/按钮。sweetalert不支持这一点。无论如何我升级到v2并完成了您的更改。现在我得到了一个错误,没有定义区域(uncaughtreferenceerror:region-notdefinedsave),请给我一个JSFIDLE,我来看看