简体   繁体   English

如何动态填充javascript函数的选项

[英]How to Dynamically populate options for javascript function

I have an application where javascript and html is largely separated. 我有一个应用程序,其中javascript和html在很大程度上是分开的。 I am trying to write something with javascript or jquery that will pull in all of the data-attributes from an html element and put those into options for a javascript function, Dynamically. 我试图用javascript或jquery写一些东西,它将从html元素中提取所有数据属性,并将它们放入javascript函数的选项中,动态地。 There are many options, every time this script is mentioned, not all will be used. 有很多选项,每次提到这个脚本时,都不会使用所有选项。

First an example, then ill show how I am doing it (semi-successfully) afterward. 首先是一个例子,然后以后的方式展示我是如何做到的(半成功)。

HTML: HTML:

<button data-behavior="sweetalert" data-title="Alert Title" data-text="Lorem ipsum dolar..." data-showCancelButton="true">Click Me</button>

Javascript: 使用Javascript:

sweetAlert({
    title: "Alert Title",
    text: "Lorem ipsum dolar...",
    showCancelButton: true,
});

Straight forward right? 直接向前吧? Ok, now lets get fancy and populate it dynamcially. 好的,现在让我们变得有趣并且动态地填充它。 So what I want is to be able to find an option in the sweetAlert api and then be able to simply attach it to my html element with a data-attribute . 所以我想要的是能够在sweetAlert api中找到一个选项,然后能够简单地将它附加到我的带有data-attribute html元素。 This way front end people can add quick html, without script tags all over the place. 这样前端人员就可以添加快速的html,而不需要脚本标签。

This is what I have done so far that actually does work and gives me the appropriate result, except with options that require non-strings (such as booleans). 这是我到目前为止所做的实际上确实有效并给我适当的结果,除了需要非字符串的选项(例如布尔值)。

$('[data-behavior="sweetalert"]').click(function() {
    attributes = $(this).data();
    delete attributes["behavior"];
    sweetAlert($.each(attributes, function(key, value) {
        key + ": " + value + ', '
    }));
});

So this function works and pulls in the title and the description correctly. 所以这个功能正常工作,并正确地提取标题和描述。 However, it does not pull in the boolean options correctly. 但是,它没有正确引入布尔选项。

Does anyone have any idea on how I can improve this so that I would be able to set boolean options (at the very least) and secondarily integer options? 有没有人知道如何改进这一点,以便我能够设置布尔选项(至少)和二次整数选项?

Surely someone out there has done this type of thing before... Thanks in advance! 当然有人在那之前做过这种事情......提前谢谢!

The jQuery .data() function automatically converts "true" to true . jQuery .data()函数自动将“true”转换为true So this should work: 所以这应该工作:

$('[data-behavior="sweetalert"]').click(function() {
    attributes = $(this).data();
    delete attributes["behavior"];
    sweetAlert(attributes);
});

data() also automatically converts integers, so you should be set on that front as well. data()也会自动转换整数,因此您也应该在该方面进行设置。

Edit to add comment from OP: data attributes are converted to camel case. 编辑以从OP添加注释:数据属性转换为驼峰大小写。 So in order for data() to produce an attribute called showCancelButton you should write it as data-show-cancel-button on the element. 因此,为了让data()生成一个名为showCancelButton的属性,您应该将其作为data-show-cancel-button在元素上。

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

相关问题 如何使用选项填充动态创建的选择 - How to populate a dynamically created select with options 如何使用 promise 动态填充下拉选项 - How to populate dropdown options dynamically using a promise 使用javascript在多选下拉列表中动态填充所选选项 - populate selected options dynamically in multiselect dropdown using javascript 使用JavaScript从PHP数组中填充带有选项的动态创建的下拉列表 - Populate Dynamically Created Dropdown with Options from PHP array using javascript 使用JavaScript jQuery使用其余选项动态填充4个下拉列表 - Dynamically populate 4 drop-down lists with the remaining options with JavaScript jQuery 如何在 javascript 中填充选择元素的选项 - How to populate the options of a select element in javascript 如何基于另一个下拉选择动态填充的下拉列表中选择? - How to dynamically populate options on dropdown lists based on selection in another dropdown? jqGrid如何根据状态数据动态填充选项列表? - How does jqGrid dynamically populate the list of options based on status data? 如何在Javascript中动态正确填充关联数组 - How to correctly populate an associative array dynamically in Javascript 如何在 JavaScript 中动态填充多维数组? - How to dynamically populate a multidimensional array in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM