1

嗨,我一直在研究这个代码,它为每个单词创建一个带有单选按钮的表格。目前效果很好,但是我没有在单选按钮上创建点击功能并提交。单击时它们都会调用被调用的函数,但是一旦在函数中,我调用getElementById以验证或更改颜色,则找不到 ID,或者至少这是我的假设,因为发出警报后,什么都没有出现。

我在 javascript 方面相对较新,所以任何建议都会有所帮助。有人告诉我,这可能是因为它们在创建之前就被调用了,但从我在代码中看到的情况来看,情况并非如此。有什么建议么?

编辑:

这是出现问题的代码。只有西班牙语有一次点击它。没有提交验证,因为我认为它们是同一个问题

for (i = 0; i < sentences.length; i++) {
    document.write('<p><a onclick ="javascript:ShowHide(\'Sentence' + (i + 1) + '\')" href="javascript:;"><font color="#0000FF"><u>Sentence' + (i + 1) + '</u></font></a></p>');
    document.write('<table style="display: none; table-layout: fixed" id="Sentence' + (i + 1) + '" border = "1">');
    writeSentence(i, words, "Sentences");
    document.write('</table>');
    document.write('<input type="checkbox" name="Sentence_lang ' + (i + 1) + '" id = "Sentence_lang ' + (i + 1) + '" value="All_English" /> All English    ');
    document.write('<input type="checkbox" name="Sentence_lang ' + (i + 1) + '" id = "Sentence_lang ' + (i + 1) + '" value="All_Spanish" /> All Spanish <br />');
}
document.write('</p>')

function writeSentence(i, array, name) {
    var Name = name;
    document.write('<tr>');
    document.write('<td>');
    document.write('</td>');
    for (j = 0; j < array[i].length; j++) {
        var word = array[i][j];
        document.write('<td>');
        if (Name == "Sentences") document.write('<span class="kept" id="word_' + i + '_' + j + '">');
        document.write(word);
        if (Name == "Sentences") document.write('</span> ');
        document.write('</td>');
    }
    document.write('</tr>');
    document.write('<td>');
    document.write('English');
    document.write('</td>');
    for (j = 0; j < array[i].length; j++) {
        document.write('<td>');
        document.write('<input type="radio" name="' + Name + (i + 1) + '_' + (j) + '" id="Eng_' + (i + 1) + '_' + (j) + '" value="English" >');
        document.write('</td>');
    }
    document.write('</tr>');
    document.write('<tr>');
    document.write('<td>');
    document.write('Spanish');
    document.write('</td>');
    for (j = 0; j < array[i].length; j++) {
        document.write('<td>');
        document.write('<input type="radio" onclick = "color(' + i + ',' + j')" name="' + Name + (i + 1) + '_' + (j) + '" id="Spa_' + (i + 1) + '_' + (j) + '" value="Spanish"> ');
        document.write('</td>');
    }
    document.write('</tr>');
    document.write('<tr>');
    document.write('<td>');
    document.write('Other');
    document.write('</td>');
    for (j = 0; j < array[i].length; j++) {
        document.write('<td>');
        document.write('<input type="radio"  name="' + Name + (i + 1) + '_' + (j) + '" id="Other_' + (i + 1) + '_' + (j) + '" value="other" > ');
        document.write('</td>');
    }
    document.write('</tr>');
    if (Name == "Sentences") for (j = 0; j < array[i].length; j++)
    document.write('<input type="radio" input style= "display: none" name="' + Name + (i + 1) + '_' + (j) + '" id="' + Name + (i + 1) + '_' + (j) + '" value="Norm" checked>');
}

function color(i, j,lang) {
    var word = document.getElementById('word_' + i + '_' + j + '');
    aleart(word);
    if (lang == "Spa") word.className = "blue";
}

编辑最后一个 if 语句是我想要做的。第三个参数目前为空,因为我无法弄清楚如何正确传递字符串 Spa。但如果需要,我可以解决这个问题。

4

1 回答 1

0

您有一个错字:aleart(word);,该错误应该在您的控制台中可见。将其更改为alert(word);并应出现警报框。

除此之外,您不应使用document.write. 一旦加载文档,它将无法工作。请参阅document.write的替代方案或document.write 的替代方案是什么?

于 2012-06-26T14:39:08.327 回答