【问题标题】:jquery focus function not working on checkboxjQuery焦点功能不适用于复选框
【发布时间】:2020-05-14 15:17:30
【问题描述】:

我有一个表单,其中包含一行中的复选框。如果未标记任何选项,我想关注该复选框。

<div class="fila-fila">
    <div class="panel">
        <div class="panel-heading">
            <p class="panel-title oblig-2">Data<span style="display: none;" class="validate_ko_enquesta_radio_button fa fa-times"></span></p>
        </div>
    </div>
    <div class="panel-body tree-col">
        <div class="row">
            <div class="col-md-6">
                <div class="well well-sm" onclick='javascript:remarcarInput("question_4_1_0_0",true,"1","0","4")'>
                    <div class="icheckbox_square-red" style="position: relative;" aria-checked="false" aria-disabled="false">
                        <input
                            value="1"
                            type="checkbox"
                            name="question_4_1_0"
                            id="question_4_1_0_0"
                            style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"
                        />
                        <ins
                            class="iCheck-helper"
                            onclick="showHideIcon(4);"
                            style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"
                        ></ins>
                    </div>
                    <label for="question_4_1_0_0" class="text-oblig">1 </label>
                </div>
            </div>
            <div class="col-md-6">
                <div class="well well-sm" onclick='javascript:remarcarInput("question_4_1_0_1",true,"1","0","4")'>
                    <div class="icheckbox_square-red" style="position: relative;" aria-checked="false" aria-disabled="false">
                        <input
                            value="2"
                            type="checkbox"
                            name="question_4_1_0"
                            id="question_4_1_0_1"
                            style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"
                        />
                        <ins
                            class="iCheck-helper"
                            onclick="showHideIcon(4);"
                            style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"
                        ></ins>
                    </div>
                    <label for="question_4_1_0_1" class="text-oblig">2 </label>
                </div>
            </div>
            <div class="col-md-6">
                <div class="well well-sm" onclick='javascript:remarcarInput("question_4_1_0_2",true,"1","0","4")'>
                    <div class="icheckbox_square-red" style="position: relative;" aria-checked="false" aria-disabled="false">
                        <input
                            value="3"
                            type="checkbox"
                            name="question_4_1_0"
                            id="question_4_1_0_2"
                            style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"
                        />
                        <ins
                            class="iCheck-helper"
                            onclick="showHideIcon(4);"
                            style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"
                        ></ins>
                    </div>
                    <label for="question_4_1_0_2" class="text-oblig">3 </label>
                </div>
            </div>
            <div class="col-md-6">
                <div class="well well-sm" onclick='javascript:remarcarInput("question_4_1_0_3",true,"1","0","4")'>
                    <div class="icheckbox_square-red" style="position: relative;" aria-checked="false" aria-disabled="false">
                        <input
                            value="4"
                            type="checkbox"
                            name="question_4_1_0"
                            id="question_4_1_0_3"
                            style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"
                        />
                        <ins
                            class="iCheck-helper"
                            onclick="showHideIcon(4);"
                            style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"
                        ></ins>
                    </div>
                    <label for="question_4_1_0_3" class="text-oblig">4</label>
                </div>
            </div>
        </div>
    </div>
</div>

然后我有一个函数,如果复选框没有任何标记的项目,则调用该函数。 传递给函数的参数是动态的。连接我获得当前 id,要关注的元素。例如:id elem: question_4_1_0_1

function checkCheckBoxShow(tipoMens,valor,numLinea,numGrup,numElem,oblig){
    var text=gEnquestaErrorSI;
    text=text.replace("#VALOR#",valor);
    $("#avisEnquestaError").html(text);
    console.log("mnsErrorRadioButton"+"#question_"+numLinea+"_"+numGrup+"_"+numElem+oblig);//params are correct

    $("#question_"+numLinea+"_"+numGrup+"_"+numElem+oblig).focus();//FOCUS NOT WORKING

    var elements = document.getElementsByClassName('validate_ko_enquesta_radio_button');
    var j = 0;
    var trobat = false;
    var requiredElement = null;
    var index = null;
    while (!trobat && j<elementsOblig.length){
        if(elementsOblig[j].iteracio == numLinea){
            trobat=true;
            index = j;
        }
        j++;
    }
        if (trobat) {
        requiredElement = elements[index];
        $(requiredElement).show();
    }
    $('.error-pagina').show();
    //$("#avisEnquestaError").css("display","block");
    $("#avisEnquestaError").css("display","inline");
    console.log("falseeeeeeeeeeeeee");
    return false;
}

如果我这样做,即使在 Firefox 控制台中:

$("#question_4_1_0_0").focus();

它不关注那个元素。

【问题讨论】:

  • 如果您发布了与您的具体问题相关的minimal reproducible example,我认为您会得到更多回复。现在代码太多,无法查看并提供任何帮助。
  • 你怎么称呼checkCheckBoxShow()
  • @Swati 像这样:jsfiddle.net/nLvbz8ha
  • 你能添加完整的html和js代码吗?这样就可以很容易地识别出问题所在。此外,在您提供的代码中,您在+numElem+oblig). 附近错过了_,这应该是+numElem+"_"+oblig);

标签: javascript jquery jsp focus


【解决方案1】:

问题在于 focus() 是一个基本的 javascript 方法,而您正试图将其应用于 jQuery 对象。

试试这个:

document.getElementById("question_4_1_0_0").focus();

【讨论】:

  • 我尝试过使用 document.getElementById("question_4_1_0_0").focus();而且它不集中:(
  • @proera 文档完全加载后函数 checkCheckBoxShow 正确触发?
  • 是的! focus() 可以正常工作,而不是类型复选框。我的表单有不同的元素,例如输入文本、单选按钮。这些元素完美地工作。我不明白为什么复选框不起作用。如果我只将复选框的 id 传递给焦点。
猜你喜欢
  • 2019-08-02
  • 2020-03-20
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 2021-04-11
相关资源
最近更新 更多