将变量从HTML传递到JS文件

时间:2015-11-11 12:07:03

标签: javascript jquery html

我正在尝试将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!"

感谢您的帮助!!!

4 个答案:

答案 0 :(得分: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');

答案 1 :(得分:1)

你在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>

答案 2 :(得分:0)

一种简单的方法是将它添加到您的id并使用一个类作为on click事件,然后您可以拆分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
});

答案 3 :(得分:-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!"