Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 来自表单输入的JS变量_Javascript_Jquery_Html_Forms_Variables - Fatal编程技术网

Javascript 来自表单输入的JS变量

Javascript 来自表单输入的JS变量,javascript,jquery,html,forms,variables,Javascript,Jquery,Html,Forms,Variables,仅供参考:出于其他原因,我必须使用数据标签作为选择器 我需要存储表单中的值,以便稍后通过js使用,这样当用户切换单选按钮时,这些值将重新插入表单输入 如何存储这些值,然后在用户单击单选按钮时重新插入它们?我现在的处境是: // user clicks initially, then the values would be stored.. $('[data-label="FrenchCheckbox"] [data-label="State1"] [data-label="Checkbox"]

仅供参考:出于其他原因,我必须使用
数据标签
作为选择器

我需要存储表单中的值,以便稍后通过js使用,这样当用户切换单选按钮时,这些值将重新插入表单输入

如何存储这些值,然后在用户单击单选按钮时重新插入它们?我现在的处境是:

// user clicks initially, then the values would be stored..
$('[data-label="FrenchCheckbox"] [data-label="State1"] [data-label="Checkbox"] [data-label="Unchecked"]').click(function() {

    // want to store the initial value now!! 
    // ..then translating the form inputs to another lang
    $('[data-label="Create StepWizard"] [data-label="CreateAssetInput"]').each(function(i, ele) {
        var tString = $(ele).val();

        if(tString.length <= 0) return;

        var tSrc = 'en';
        var tDst = 'fr';
        var key = 'my api key';
        var tUrl = "https://www.googleapis.com/language/translate/v2?key=" + key + "&source=" + tSrc + "&target=" + tDst + "&q=" + tString;
        $.get(tUrl, function(response) {
            $(ele).val(response.data.translations[0].translatedText);
        });
    });
});

谢谢你的想法

一种简单的方法是使用jQuery
data()

然后重置:

 /* within event handler */
 $(this).val( $(this).data('default') );

简单的javascript对象有什么问题吗?没什么问题,我只是在寻找一个有效的解决方案。我对js/jquery不是很有经验,所以不知道如何构造代码来实现我的目标。如果你能给我举个例子,我将不胜感激!谢谢感谢@charlietfl的反馈。你能帮我弄清楚如何将其集成到上面的示例中吗?我对js/jquery不是很有经验。感谢您提供的任何见解。我不能100%确定您想要什么行为,因为您正在对元素使用2个单击处理程序。
// want to store the initial value now!! 
$(this).data('default', $(this).val());
 /* within event handler */
 $(this).val( $(this).data('default') );