Javascript Value composed of several words [value=

2019-08-02 20:02发布

thanks for the help in this forum, I was able to get my code to work:

// ==UserScript==
// @name        PARINGO
// @description Auto select rpt radio button
// @namespace   PARINGO
// @include     https://docs.google.com/spreadsheet/viewform?formkey=dG1fdUU1bTR5R1l3dmtGS095QVYxZlE6MQ
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant       GM_addStyle
// @version     1
// ==/UserScript==
/*- The @grant directive is needed to work around a design change introduced
    in GM 1.0.   It restores the sandbox.
*/
//-- Note that :contains() is case-sensitive
var questionLabel   = $(
    "label.ss-q-title:contains('How much is 1+1') ~ ul.ss-choices"
).first ().find ("input[type=radio][value=2]");
questionLabel.prop ('checked', true);

var questionLabel   = $(
    "label.ss-q-title:contains('How much is 4+2') ~ ul.ss-choices"
).first ().find ("input[type=radio][value=6]");
questionLabel.prop ('checked', true);

What makes this script is to check the label and mark the correct answer. But if the response is composed of two or more separate words. No works e.g:

var questionLabel   = $(
        "label.ss-q-title:contains('How much is 4+2') ~ ul.ss-choices"
    ).first ().find ("input[type=radio][value=dog crazy]");
questionLabel.prop ('checked', true);

This works for the form in google docs: Form Google Docs

Anybody can tell me how do that the value accept separate words?

("input[type=radio][value=dog crazy]")

3条回答
在下西门庆
2楼-- · 2019-08-02 20:13

Try putting the value in quotes

"input[type=radio][value='dog crazy']"
查看更多
一纸荒年 Trace。
3楼-- · 2019-08-02 20:24

You are using jQuery's find method. Put the values in quotes:

"input[type=radio][value='dog crazy']"

There are examples like this in the jQuery documentation for Attributes in selectors. For example:

  • double quotes inside single quotes: $('a[rel="nofollow self"]')
  • single quotes inside double quotes: $("a[rel='nofollow self']")

Specifically, the docs state:

Attribute values in selector expressions must follow the rules for W3C CSS selectors, in general that means anything other than a simple identifier should be surrounded by quotation marks.

查看更多
贪生不怕死
4楼-- · 2019-08-02 20:26

If an attribute consists of several words than you have to use quotes like this:

"input[type=radio][value='dog crazy']"
查看更多
登录 后发表回答