Javascript Value composed of several words [value=dog crazy]

人盡茶涼 提交于 2019-12-11 09:29:35

问题


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]")

回答1:


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

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



回答2:


Try putting the value in quotes

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



回答3:


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.



来源:https://stackoverflow.com/questions/13333094/javascript-value-composed-of-several-words-value-dog-crazy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!