2015-11-11 54 views
1

我试图将HTML页面中的值传递给JS文件。将HTML中的变量传递给JS文件

HTML部分:

<a href="#" id="pagejs_general_delete_wizardresultid"><i class=" icon-bin" ></i> Delete</a> 

JS文件:

$('#pagejs_general_delete_wizardresultid').on('click', function() { 
     swal({ 
      title: "Are you sure?", 
      text: "Are you sure you want to delete item with reference <wizardresultid here>? This can not be undone!", 
      type: "warning", 
      showCancelButton: true, 
      confirmButtonColor: "#EF5350", 
      confirmButtonText: "Yes, delete it!", 
      cancelButtonText: "No, cancel!", 
      closeOnConfirm: false, 
      closeOnCancel: false 
     }, 
     function(isConfirm, wizardresultid){   
      if (isConfirm) { 
       swal({ 
        title: "Deleted!", 
        text: "The record has been deleted.", 
        confirmButtonColor: "#66BB6A", 
        type: "success" 
       }); 
      } 
      else { 
       swal({ 
        title: "Cancelled", 
        text: "Nothing has been changed.", 
        confirmButtonColor: "#2196F3", 
        type: "error" 
       }); 
      } 
     }); 
    }); 

我不确定我怎么能传递变量wizardresultid从HTML的JavaScript。我可以找到如何从按钮传递一个函数的例子,但不知道如何从链接中完成它。 此外,我试图在文本中显示wizardresultid。是这样做的正确方法:

text: "Are you sure you want to delete item with reference" + wizardresultid + "? This can not be undone!" 

感谢您的帮助!

+0

其实我不看wizardresultid的任何暗示在你的HTML。但是,这可能是你要找的东西? :http://stackoverflow.com/questions/3273350/jquery-click-pass-parameters-to-user-function – Gimby

回答

1

您shold在HTML中使用数据属性,并与JS得到这个。 ATTR()。

$('#pagejs_general_delete_wizardresultid').on('click', function() { 
 
    var myAttribute = $(this).attr('wizardresultid'); 
 
    
 
     swal({ 
 
      title: "Are you sure?", 
 
      text: "Are you sure you want to delete item with reference "+myAttribute+"? This can not be undone!", 
 
      type: "warning", 
 
      showCancelButton: true, 
 
      confirmButtonColor: "#EF5350", 
 
      confirmButtonText: "Yes, delete it!", 
 
      cancelButtonText: "No, cancel!", 
 
      closeOnConfirm: false, 
 
      closeOnCancel: false 
 
     }, 
 
     function(isConfirm, wizardresultid){   
 
      if (isConfirm) { 
 
       swal({ 
 
        title: "Deleted!", 
 
        text: "The record has been deleted.", 
 
        confirmButtonColor: "#66BB6A", 
 
        type: "success" 
 
       }); 
 
      } 
 
      else { 
 
       swal({ 
 
        title: "Cancelled", 
 
        text: "Nothing has been changed.", 
 
        confirmButtonColor: "#2196F3", 
 
        type: "error" 
 
       }); 
 
      } 
 
     }); 
 
    });
<a href="#" id="pagejs_general_delete_wizardresultid" wizardresultid="55"><i class=" icon-bin" ></i> Delete</a>

+0

这很完美。非常感谢! – mitch2k

0

一个简单的方法是将其添加到您的ID和使用类的点击事件,然后你可以拆分ID。

类似:

//html 
<a href="#" class="pagejs_general_delete_wizardresultid" id="result-1"><i class=" icon-bin" ></i> Delete</a> 

//js 
$('.pagejs_general_delete_wizardresultid').on('click', function() { 
    var splitId = $(this).attr('id'); 
    splitId = splitId.split('-'); 
    var realId = splitId[1] 

    //.....rest of your logic goes here 
}); 
5

推荐 '数据' 属性。像使用

<a href="#" id="pagejs_general_delete_wizardresultid" data="your data"><i class=" icon-bin" ></i> Delete</a> 

和访问:

var val = $('#pagejs_general_delete_wizardresultid').attr('data'); 
+0

是的,这是比我的更好的方法... – FabioG

+0

尽管这是一个更加用户友好的方式它(因为你可以使用用户友好的名称而不是元素ID来引用元素),它实际上并不回答OP的问题 – Precastic

-1

wizardresultid处于SweetAlert回调函数的第二个参数。您不能在该特定回调函数外引用该参数。

相反,你应该使用$(this).attr('id')它得到目标元素的ID在点击回调函数,如:

text: "Are you sure you want to delete item with reference" + $(this).attr('id') + "? This can not be undone!" 
+0

这不是OP所要求的。 OP需要元素ID的最后部分 - 不是所有的ID。 –

+0

@ZakariaAcharki如果你看看OPs代码,他所指的“wizardresultid”是回调函数中的参数,如果你看SweetAlert网站的话就是元素的完整ID。无处不在,他提到了身份证的一部分。 – Precastic

+0

请参阅公认的答案,您将会理解。 –