简体   繁体   English

将变量从HTML传递到JS文件

[英]Pass a variable from HTML to a JS file

I'm trying to pass a value from a html page to a JS file. 我正在尝试将html页面中的值传递给JS文件。

HTML part: HTML部分:

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

JS file: 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"
                });
            }
        });
    });

I'm unsure how I can pass the variable wizardresultid from HTML to the javascript. 我不确定如何将变量wizardresultid从HTML传递给javascript。 I can find examples how to pass it a function from a button, but not how to do it from a link. 我可以找到一些示例,该示例如何通过按钮将其传递给函数,而不是如何通过链接进行传递。 Furthermore, I'm trying to display the wizardresultid in the text. 此外,我正在尝试在文本中显示wizardresultid Is that the correct way to do that: 那是这样做的正确方法:

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

Thanks for your help!!! 谢谢你的帮助!!!

Recommended 'data' attribute. 推荐的“数据”属性。 like 喜欢

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

and access using: 并使用以下方式访问:

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

You shold use data-attribute in html, and get this in js with .attr(). 您可以在html中使用data-attribute,并通过.attr()在js中获取它。

 $('#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> 

a simple way would be to add it to your id and use a class for the on click event, then you could split the id. 一种简单的方法是将其添加到您的ID中,并为on click事件使用一个类,然后可以拆分ID。

something like: 就像是:

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

wizardresultid is the second parameter in the SweetAlert callback function. Wizardresultid是SweetAlert回调函数中的第二个参数。 You cannot reference that parameter outside that particular callback function. 您不能在特定的回调函数之外引用该参数。

Instead you should be using $(this).attr('id') which gets the ID of the target element in the click callback function, as in: 相反,您应该使用$(this).attr('id')来获取点击回调函数中目标元素的ID,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM