我必须刷新页面才能运行我的脚本。需要对我的代码执行什么操作才能使其在不刷新的情况下正常工作?
HTML
<div id="lblSoru" align="center" style="position: relative; top: 100px; font-size:20px; font-family: proxima_nova_rgregular; color:" class="font">Which country is bigger than another</div>
<td><input id="rdCevaplar_0" type="radio" name="rdCevaplar" value="1" onclick="javascript:setTimeout('__doPostBack(\'rdCevaplar$0\',\'\')', 0)" /><label for="rdCevaplar_0">France</label></td>
</tr><tr>
<td><input id="rdCevaplar_1" type="radio" name="rdCevaplar" value="2" onclick="javascript:setTimeout('__doPostBack(\'rdCevaplar$1\',\'\')', 0)" /><label for="rdCevaplar_1">Germany</label></td>
</tr><tr>
<td><input id="rdCevaplar_2" type="radio" name="rdCevaplar" value="3" onclick="javascript:setTimeout('__doPostBack(\'rdCevaplar$2\',\'\')', 0)" /><label for="rdCevaplar_2">Romania</label></td>
</tr><tr>
<td><input id="rdCevaplar_3" type="radio" name="rdCevaplar" value="4" onclick="javascript:setTimeout('__doPostBack(\'rdCevaplar$3\',\'\')', 0)" /><label for="rdCevaplar_3">Greece</label></td>
</tr>
油猴代码:
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
var answerKey = [
{ q: "Which country is bigger than another", a: "Germany" }
, { q: "Best SciFi franchise", a: "Star Trek" }
, { q: "Most badass monster", a: "Bun-Bun" }
, { q: "Most toxic chemical in this list", a: "Mountain Dew" }
// etc.
];
//--- Loop through the question(s) on the page and answer then if possible.
var questionTxt = $("#lblSoru");
questionTxt.each ( function () {
var bFoundAnswer = false;
var answerTxt = getAnswer (this.textContent);
if (answerTxt) {
//--- We have the answer text, now find the matching radio button and select it.
var ansForThisQ = $("label");
console.log ("getAnswer: ", ansForThisQ);
ansForThisQ.each ( function () {
var zRegExp = new RegExp (answerTxt, 'i');
if (zRegExp.test (this.textContent) ) {
bFoundAnswer = true;
var label = $(this);
var radioButt = $("#" + label.prop ("for") );
radioButt.prop ("checked", "checked");
label.css ("background", "lime");
return false; // End loop
}
} );
}
else {
alert ("I don't know how to answer: '" + this.textContent + "'");
$(this).css ("background", "pink");
}
if ( answerTxt && ! bFoundAnswer) {
alert ("The page does not have the specified answer for the question: '" + this.textContent + "'");
$(this).css ("background", "pink");
}
} );
function getAnswer (questionText) {
for (var J = answerKey.length - 1; J >= 0; --J) {
var zRegExp = new RegExp (answerKey[J].q, 'i');
if (zRegExp.test (questionText) )
return answerKey[J].a;
}
return null;
}