如何使用 Javascript 在 RadioButton 中显示正确答案

如何使用 Javascript 在 RadioButton 中显示正确答案

我需要点击(显示答案)按钮并输入正确的密码时显示正确的答案。

为此,请在单选按钮中选择正确答案(true)。

我使用 MikTeX PDFLatex(JavaScript)和 Acrobat Reader。

提前致谢。

WME:

\documentclass[12pt]{article}
\usepackage{amsmath, amssymb, latexsym, mathtext}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, left=20mm, right=20mm, top=20mm, bottom=20mm]{geometry}
\usepackage{enumitem}
%----------------------------
\usepackage[pdftex]{eforms}
\everyRadioButton{}
%
\newcounter{nvopr}
\newcommand{\VOPRN}{\addtocounter{nvopr}{1}\arabic{nvopr}}%
%
\makeatletter
%
\renewcommand{\alph}[1]{\asbuk{#1}}
\setenumerate[1]{label=\arabic*), nosep, leftmargin=1.55\parindent, labelindent=\parindent, labelsep=*}
\setenumerate[2]{label=\theenumi\arabic*), nosep, leftmargin=\parindent,  listparindent=\parindent}
\makeatother
%----------------------------
\begin{document}
\begin{center}
    \LARGE Test\ldots
\end{center}

\VOPRN. The application of the information system implements?
\par\bigskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{false} secure storage of information;
\par\medskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{false} performing application-specific transformations of information and calculations;
\par\medskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{false} providing users with a convenient interaction interface;
\par\medskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{true} all of the above.
\par\bigskip
\medskip\hrule height 1pt\medskip
\par\bigskip

\VOPRN. What data model does XML implement?
\par\bigskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{true} hierarchical;
\par\medskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{false} network;
\par\medskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{false} relational.
\par\bigskip
\medskip\hrule height 1pt\medskip
\par\bigskip
    
    \begin{center}
        \pushButton[\CA{Result} \A{/S/JavaScript/JS(
            var correct = 0;
            var amount = \arabic{nvopr};% 19
            var est = 0;
            for( var i = 1; i <= amount; i++ ){
                var selected = this.getField("button"+i).value;
                if(selected===true) {
                    this.getField("button"+i).textColor=color.green;
                    correct++;
                } else {
                    this.getField("button"+i).textColor=color.red;
                }
            }
            var proc = (correct*100/amount).toFixed(0);
            if (proc >= 0 && proc < 50) {
                est = 2;
            } else if (proc >= 50 && proc < 65) {
                est = 3;
            } else if (proc >= 65 && proc < 85) {
                est = 4;
            } else /* it is greater than 85 */ {
                est = 5;
            }
            app.alert('Correct answers ' + correct + ' from ' + amount + ' (' + proc + String.fromCharCode(37) + ') ' + 'Rating - ' + est);
            )}]{jsEx}{100bp}{11bp}
\hspace{5em}
\pushButton[\CA{Show answers} \A{/S/JavaScript/JS(
%
%   Show correct answers after entering password
%
)}]{jsEx}{100bp}{11bp}
    \end{center}
\end{document}

在此处输入图片描述

答案1

您可以使用该函数提示用户输入一个字符串execDialog()。然后您可以测试该字符串是否与另一个字符串(密码)相同。输入正确的密码后,将通过将单选按钮组的值设置为“true”来选择正确的答案。

但请注意,这根本不安全!任何拥有 Acrobat Pro 的人都可以轻松访问密码,因为它在脚本中是硬编码的(即“MySecretPassword”)。

还要注意,虽然属于同一组的所有单选按钮都应具有相同的 ID,但最好为每个按钮赋予不同的值。两个按钮实际上应该具有不同的 ID。

\documentclass[12pt]{article}
\usepackage{amsmath, amssymb, latexsym, mathtext}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, left=20mm, right=20mm, top=20mm, bottom=20mm]{geometry}
\usepackage{enumitem}
%----------------------------
\usepackage[pdftex]{eforms}
\everyRadioButton{}
%
\newcounter{nvopr}
\newcommand{\VOPRN}{\addtocounter{nvopr}{1}\arabic{nvopr}}%
%
\makeatletter
%
\renewcommand{\alph}[1]{\asbuk{#1}}
\setenumerate[1]{label=\arabic*), nosep, leftmargin=1.55\parindent, labelindent=\parindent, labelsep=*}
\setenumerate[2]{label=\theenumi\arabic*), nosep, leftmargin=\parindent,  listparindent=\parindent}
\makeatother
%----------------------------
\begin{document}
\begin{center}
    \LARGE Test\ldots
\end{center}

\VOPRN. The application of the information system implements?
\par\bigskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{false1} secure storage of information;
\par\medskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{false2} performing application-specific transformations of information and calculations;
\par\medskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{false3} providing users with a convenient interaction interface;
\par\medskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{true} all of the above.
\par\bigskip
\medskip\hrule height 1pt\medskip
\par\bigskip

\VOPRN. What data model does XML implement?
\par\bigskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{true} hierarchical;
\par\medskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{false1} network;
\par\medskip \radioButton{button\arabic{nvopr}}{10bp}{10bp}{false2} relational.
\par\bigskip
\medskip\hrule height 1pt\medskip
\par\bigskip
    
    \begin{center}
        \pushButton[\CA{Result} \A{/S/JavaScript/JS(
            var correct = 0;
            var amount = \arabic{nvopr};% 19
            var est = 0;
            for( var i = 1; i <= amount; i++ ){
                var selected = this.getField('button'+i).value;
                if(selected === true) {
                    this.getField('button'+i).textColor = color.green;
                    correct++;
                } else {
                    this.getField('button'+i).textColor = color.red;
                }
            }
            var proc = (correct*100/amount).toFixed(0);
            if (proc >= 0 && proc < 50) {
                est = 2;
            } else if (proc >= 50 && proc < 65) {
                est = 3;
            } else if (proc >= 65 && proc < 85) {
                est = 4;
            } else /* it is greater than 85 */ {
                est = 5;
            }
            app.alert('Correct answers ' + correct + ' from ' + amount + ' (' + proc + String.fromCharCode(37) + ') ' + 'Rating - ' + est);
            )}]{jsEx1}{100bp}{11bp}
\hspace{5em}
\pushButton[\CA{Show answers} \A{/S/JavaScript/JS(
var pwDlg = {
    description: {
        elements: [
            { name: "Enter password to show answers:", type: "static_text" },
            { item_id: "txt1", type: "edit_text", char_width: 50, password: true },
            { type: "ok_cancel" },
       ]
    },
    commit: function(dialog) {
        var data = dialog.store();
        this.strPw = data['txt1'];
    }
};
if(app.execDialog(pwDlg) == 'ok' && pwDlg.strPw == 'MySecretPassword') {
    var amount = \arabic{nvopr};
    for( var i = 1; i <= amount; i++ ){
        this.getField('button'+i).value = true;
        this.getField('button'+i).textColor = color.green;
        this.getField('button'+i).readonly = true;
    }   
    app.beep();
}
)}]{jsEx2}{100bp}{11bp}
    \end{center}

\end{document}

在此处输入图片描述

(由于我的工作环境的设置,图片上对话框中的标签是德语的。它们将采用用户相关工作环境的语言。)


为了使一切变得更加不透明(但不是真正安全),您可以添加一个哈希函数(取自这里)将用户输入的哈希值与密码的哈希值进行比较:

(请注意,在这种实现中,随机字符串可能会意外产生相同的哈希值。为了获得更好的安全性,需要使用安全且抗碰撞的哈希算法,例如 SHA。)

function getHash(string) {
    var hash = 0;
    if (string.length == 0) {
        return hash;
    }
    for (i = 0; i < string.length; i++) {
        char = string.charCodeAt(i);
        hash = ((hash << 5) - hash) + char;
        hash = hash & hash;
    }
    return hash;
}
var pwDlg = {
    description: {
        elements: [
            { name: "Enter password to show answers:", type: "static_text" },
            { item_id: "txt1", type: "edit_text", char_width: 50, password: true },
            { type: "ok_cancel" },
       ]
    },
    commit: function(dialog) {
        var data = dialog.store();
        this.strPw = data['txt1'];
    }
};
if (app.execDialog(pwDlg) == 'ok' && getHash(pwDlg.strPw) == -904298921) {
    var amount = \arabic{nvopr};
    for (var i = 1; i <= amount; i++) {
        this.getField('button'+i).value = true;
        this.getField('button'+i).textColor = color.green;
        this.getField('button'+i).readonly = true;
    }   
    app.beep();
}

相关内容