简体   繁体   English

从设置变量 <input> 值

[英]Set variable from <input> value

I need to implement Google Analytics (Universal/analytics.js) on a 20-something part AJAX-based questionnaire. 我需要在20左右的基于AJAX的调查问卷上实施Google Analytics(分析)(Universal / analytics.js)。 The questionnaire works something like a choose-your-own-adventure, whereas the follow-up questions are all determined by the preceding answers. 问卷的工作方式类似于自行选择冒险,而后续问题均由前面的答案确定。

Each input has a name and an ID. 每个输入都有一个名称和一个ID。 I'm interested in pulling the value from this field and setting it as a global variable in a dataLayer. 我有兴趣从此字段中提取值并将其设置为dataLayer中的全局变量。 Everything has a unique ID and Name, which I've started collecting in a database. 一切都有唯一的ID和名称,我已经开始在数据库中收集它们。

Here's an example of the HTML: 这是HTML的示例:

<table id="ctl00_ContentPlaceHolder1_2_Table1" class="CompSSRadio" cellpadding="1" cellspacing="1">
            <tr>
                <td class="CompSSRadioLabel">
                </td>
            </tr>
            <tr>
                <td class="CompSSRadioResponses">
                <table id="ctl00_ContentPlaceHolder1_2_1_48_1" class="CompSSRadioResponses" border="0">
                    <tr>
                        <td><input id="ctl00_ContentPlaceHolder1_2_1_48_1_0" type="radio" name="ctl00$ContentPlaceHolder1$2$1_48_1" value="4" /><label for="ctl00_ContentPlaceHolder1_2_1_48_1_0"><SPAN style="FONT-FAMILY: Museo 500"><SPAN style="FONT-SIZE: 14pt"><SPAN style="FONT-FAMILY: Museo 500"></SPAN>Test answer 1</SPAN></SPAN></label></td>
                    </tr><tr>
                        <td><input id="ctl00_ContentPlaceHolder1_2_1_48_1_1" type="radio" name="ctl00$ContentPlaceHolder1$2$1_48_1" value="5" /><label for="ctl00_ContentPlaceHolder1_2_1_48_1_1"><SPAN style="FONT-FAMILY: Museo 500"><SPAN style="FONT-SIZE: 14pt"><SPAN style="FONT-FAMILY: Museo 500"></SPAN>Test answer 2</SPAN></SPAN></label></td>
                    </tr>
                </table>
            </td>
            </tr>
        </table>

Here is the "next" button: 这是“下一步”按钮:

<input type="image" name="ctl00$ContentPlaceHolder1$btnNext2" id="ctl00_ContentPlaceHolder1_btnNext2" src="../App_Themes/Images/Right.gif" onclick="javascript: pageTracker._trackPageview('//forward.html');window.document.getElementById('ctl00_ContentPlaceHolder1_btnNext').disabled=true;window.document.getElementById('ctl00_ContentPlaceHolder1_btnNext2').disabled=true;__doPostBack('ctl00$ContentPlaceHolder1$btnNext2','');" style="border-width:0px;" />

And here is a script which does something with the form info: 这是一个脚本,可以对表单信息进行处理:

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

I don't know if I need to hook into that or anything, since that's what's submitting information. 我不知道我是否需要参与其中,因为这就是提交信息的目的。 Obviously I'm clueless. 显然我很笨。 I feel like this is a fairly simple task, it's just my lack of javascript knowledge preventing me from crafting the necessary script. 我觉得这是一个相当简单的任务,只是我缺乏JavaScript知识而无法编写必要的脚本。

Thanks ahead of time! 提前谢谢!

IF you are using radios to select one of multiple values, all radio elements must have the same name. 如果使用无线电选择多个值之一,则所有无线电元素必须具有相同的名称。 For example, if you have: 例如,如果您有:

<input type="radio" name="sex" value="Male" id="radio_male">
<label for="radio_male">Male</label>

<input type="radio" name="sex" value="Female" id="radio_female">
<label for="radio_female">Female</label>

You will only be able to select "Male" or "Female", but not both. 您将只能选择“男性”或“女性”,但不能两者都选择。 Taking that in consideration, the way to access the selected value, in Javascript, should be: 考虑到这一点,使用Javascript访问所选值的方式应为:

var elements = document.getElementsByName("sex");

for (i = 0; i < elements.length; i++) {
    var el = elements[i];

    if ( el.checked ) {
        alert("Checked value: " + el.value);
        break; // Because only one radio from the group will be checked, there's no point in checking the rest of them, if there are more
    }
}

About Data Layer, I have no clue, but it sounds like you are having problems getting the values, if I'm understanding it well. 关于数据层,我一无所知,但是,如果我对它很了解的话,听起来您在获取值方面遇到问题。

If you're open to jQuery, an even simpler method would be: 如果您愿意使用jQuery,则更简单的方法是:

var dataObj = {};

// Get the name / value pair of the checked radio button
jQuery('input:checked').each(
    function() {
        dataObj[$(this).attr("name")] = $(this).val();
    }  
);

etc. 等等

There's several techniques you could use to move through the fields in order. 您可以使用多种技术来按顺序遍历各个字段。 With a bit more context in the question (as in what the behavior is from one question to the next - can you get the next element ID from your ajax call, for example?), a more robust answer could get you pointed in the right direction. 在问题中包含更多上下文(例如,从一个问题到下一个问题的行为是什么-例如,您可以从ajax调用中获取下一个元素ID吗?),更可靠的答案可以使您指向正确的位置方向。

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

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