Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.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 用Greasemonkey编译一系列单选按钮?_Javascript_Jquery_Forms_Greasemonkey - Fatal编程技术网

Javascript 用Greasemonkey编译一系列单选按钮?

Javascript 用Greasemonkey编译一系列单选按钮?,javascript,jquery,forms,greasemonkey,Javascript,Jquery,Forms,Greasemonkey,我必须按照以下顺序多次编译一个表格,如下图所示: 第一个问题A 第二个问题D 第三个问题B 第四个问题D 第五个问题D 使用Greasemonkey,但到目前为止,我还没有发现如何在web上: 你能帮我吗 注意:现在似乎OP在使用Compile时表示完成。这给了这个问题非常不同的味道!我已要求OP提出一个新问题-关于使用Greasemonkey在第三方网站上填写表格: 你的问题来自哪里 对于这个问题,我们将把它们硬编码到Greasemonkey脚本中。 实际上,您可能希望从服务器获取它们 鉴于这

我必须按照以下顺序多次编译一个表格,如下图所示:

第一个问题A 第二个问题D 第三个问题B 第四个问题D 第五个问题D 使用Greasemonkey,但到目前为止,我还没有发现如何在web上: 你能帮我吗

注意:现在似乎OP在使用Compile时表示完成。这给了这个问题非常不同的味道!我已要求OP提出一个新问题-关于使用Greasemonkey在第三方网站上填写表格:


你的问题来自哪里

对于这个问题,我们将把它们硬编码到Greasemonkey脚本中。 实际上,您可能希望从服务器获取它们

鉴于这些数据:

var QandA   = [ { question: "Question 1",
                  answers:  ["Answer 1", "Answer 2"],
                  correct:  [2]
                },
                { question: "Isn't life without geometry pointless?",
                  answers:  ["Yes", "Maybe", "That's Squaresville, Daddy-o."],
                  correct:  [2,3]
                }
            ];
然后,此代码将在页面中插入测验:

将其全部放在Greasemonkey脚本中,并添加样式:
双重行动!第一次行动是针对错误的问题,第二次行动是因为我无法找到问题答案链接。。。我是个可怕的人:|没问题。提出第二个问题,我们就能更好地提供帮助。不过有一件事。看起来你想要的单词是完整的。Compile具有不同的含义,尤其是在这种情况下:好的,第三个ops是针对我的英语XD新问题创建的,我希望我更清楚。你太好了!:没问题。你的英语比我填的好。谢谢你提出另一个问题。你想要什么是可能的,但这在很大程度上取决于页面的细节。我很快就要结束了,但是如果没有人比我更努力的话,我明天就要着手解决这个问题了。
//--- Build the Q&A HTML, with jQuery.
var htmlSrc     = '';

//--- Process one question at a time.
$.each (QandA, function (questIdx, questionObj) {
    var questNum    = questIdx + 1;
    htmlSrc    += '<h2><span class="GM_QuestNum">' + questNum + '</span>' + questionObj.question
                + '<span class="GM_Result">&nbsp;</span></h2>'
                + '<ul correctAns="' + questionObj.correct + '">';

    //--- Process one answer at a time.
    $.each (questionObj.answers, function (ansIdx, ansText) {
        var ansNum  = ansIdx + 1;
        htmlSrc    += '<li><label>'
                    + '<input type="radio" value="' + ansNum + '" name="GM_Quest_' + questIdx + '">'
                    + ansText
                    + '</label></li>'
                    ;
    } );
    htmlSrc    += '</ul>';
} );

//--- Generate the container and populate it with our question HTML.
$('body').append (      '<div id="GM_OurCustomQuiz">'
                    +   '<h1>Enjoy our fun Quiz!</h1>'
                    +   htmlSrc
                    +   '<button id="GM_CustQuizScoreBtn">Check Answers</button>'
                    +   '</div>'
                 );

//--- Make the score button live.
$('#GM_CustQuizScoreBtn').click ( function () {
    //--- Loop through the questions and check the answers.
    $('#GM_OurCustomQuiz ul').each ( function () {
        var jThis       = $(this);  /*-- "this" is a special variable inside each()
                                        Here it refers to the <ul> node for this
                                        loop iteration.
                                    */
        var ansVal      = jThis.find ("input:radio:checked").val ();
        var correctAns  = jThis.attr ("correctAns");
        var resultBox   = jThis.prev ('h2').children ('.GM_Result');
        if ($.inArray (ansVal, correctAns) >= 0 )
            //-- Pass! (That code is the HTML checkmark.)
            resultBox.html ('&#10003;').removeClass ('GM_WrongAns');
        else
            resultBox.html ('X').addClass ('GM_WrongAns');
    } );
} );
// ==UserScript==
// @name            _Quiz Display
// @include         http://www.google.com/*
// @include         http://stackoverflow.com/questions/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==

//--- This is our quiz data.
var QandA   = [ { question: "Question 1",
                  answers:  ["Answer 1", "Answer 2"],
                  correct:  [2]
                },
                { question: "Isn't life without geometry pointless?",
                  answers:  ["Yes", "Maybe", "That's Squaresville, Daddy-o."],
                  correct:  [2,3]
                }
            ];


//--- Build the Q&A HTML, with jQuery.
var htmlSrc     = '';

//--- Process one question at a time.
$.each (QandA, function (questIdx, questionObj) {

    var questNum    = questIdx + 1;
    htmlSrc    += '<h2><span class="GM_QuestNum">' + questNum + '</span>' + questionObj.question
                + '<span class="GM_Result">&nbsp;</span></h2>'
                + '<ul correctAns="' + questionObj.correct + '">';

    //--- Process one answer at a time.
    $.each (questionObj.answers, function (ansIdx, ansText) {

        var ansNum  = ansIdx + 1;
        htmlSrc    += '<li><label>'
                    + '<input type="radio" value="' + ansNum + '" name="GM_Quest_' + questIdx + '">'
                    + ansText
                    + '</label></li>'
                    ;
    } );
    htmlSrc    += '</ul>';
} );


//--- Generate the container and populate it with our question HTML.
$('body').append (      '<div id="GM_OurCustomQuiz">'
                    +   '<h1>Enjoy our fun Quiz!</h1>'
                    +   htmlSrc
                    +   '<button id="GM_CustQuizScoreBtn">Check Answers</button>'
                    +   '</div>'
                 );


//--- Make the score button live.
$('#GM_CustQuizScoreBtn').click ( function () {

    //--- Loop through the questions and check the answers.
    $('#GM_OurCustomQuiz ul').each ( function () {
        var jThis       = $(this);  /*-- "this" is a special variable inside each()
                                        Here it refers to the <ul> node for this
                                        loop iteration.
                                    */
        var ansVal      = jThis.find ("input:radio:checked").val ();
        var correctAns  = jThis.attr ("correctAns");
        var resultBox   = jThis.prev ('h2').children ('.GM_Result');

        if ($.inArray (ansVal, correctAns) >= 0 )
            //-- Pass! (That code is the HTML checkmark.)
            resultBox.html ('&#10003;').removeClass ('GM_WrongAns');
        else
            resultBox.html ('X').addClass ('GM_WrongAns');
    } );
} );



//--- Use CSS styles to make everything look pretty.
GM_addStyle ( (<><![CDATA[
    #GM_OurCustomQuiz {
        color:                  black;
        background:             gold;
        text-align:             left;
        font-size:              16px;
        font-weight:            normal;
        font-family:            "Trebuchet MS", Helvetica, Tahoma, Arial, sans-serif;
        padding:                10px 5%;
        line-height:            1.5;
        max-width:              60%;
        max-height:             750px;
        opacity:                0.95;
        position:               absolute;
        padding:                0ex 3ex 1ex 3ex;
        top:                    0.5ex;
        left:                   0.5ex;
        overflow-x:             hidden;
        overflow-y:             auto;
        border:                 3px outset black;
    }
    #GM_OurCustomQuiz h1, h2 {
        line-height:            1.2;
        margin-bottom:          0.9em;
        font-weight:            bold;
    }
    #GM_OurCustomQuiz h1 {
        font-size:              2em;
        text-shadow:            0px 2px 3px #555;
    }
    #GM_OurCustomQuiz h2 {
        font-size:              1.2em;
    }
    #GM_OurCustomQuiz input[type=radio] {
        margin-right:           0.7ex;
    }
    #GM_OurCustomQuiz label {
        cursor:                 pointer;
    }
    #GM_OurCustomQuiz button {
        margin:                 1em;
        padding:                1ex;
        font-size:              120%;
        cursor:                 pointer;
        background:             buttonface;
        border:                 4px outset buttonface;
        border-radius:          8px 8px 8px 8px;
        box-shadow:             2px 2px 4px gray;
    }
    #GM_OurCustomQuiz button:hover {
        color:                  red;
        text-shadow:            0 0 3px #F66;
    }
    #GM_OurCustomQuiz ol, ul {
        list-style-type:        none;
        list-style-image:       none;
        list-style-position:    outside;
        padding-left:           3ex;
        margin-top:             -1ex;
    }
    .GM_QuestNum {
        margin-right:           1ex;
    }
    .GM_QuestNum:after {
        content:                ')';
    }
    .GM_Result {
        color:                  lime;
        display:                inline-block;
        font-family:            comic Sans MS;
        font-size:              4ex;
        font-weight:            bold;
        margin:                 0 0 0 6px;
        text-shadow:            2px 2px 5px;
        width:                  32px;
        height:                 44px;
        padding:                0;
    }
    .GM_WrongAns {
        color:                  red;
        font-weight:            normal;
    }
]]></>).toString () );