jQuery 手风琴 - 设置活动手风琴的输入焦点?

发布于 2024-08-24 04:55:40 字数 2167 浏览 4 评论 0原文

**没关系。我想通了。 **

我是这样做的:

$("#accordion").accordion({
        header:'h3',
        active: '#section1',
        autoheight: false,
        clearstyle: true,
}).bind("change.ui-accordion", function(event,ui) {
    $("#text1").focus();
});

我已经设置好了一个手风琴,并且每个 div 中都有一个表单。我只是想弄清楚如何根据打开的输入字段将焦点设置在输入字段上...

/* accordion */
$("#accordion").accordion({
        header:'h3',
        active: '#section1',
        autoheight: false,
        clearstyle: true
});

基本上,我想将光标设置在打开的部分的第一个输入字段中。实际的形式要大得多,所以我把它压缩得非常大......

    <div id="accordion">
        <h3 id="section1"><a href="#">Section 1/a></h3>
        <div>
            <form id="form1" action="form.php" method="post">
                <fieldset class="inside">
                    <input type="text" name="text1" id="text1" size="50" value="Default text" />
                    <input class="open" type="button" value="Submit" name="submit1" />
                </fieldset>
            </form>
        </div><!--/div-->

        <h3 id="section2"><a href="#">Section 2</a></h3>
        <div>
            <form id="form2" action="form.php" method="post">
                <fieldset class="inside">
                    <input type="text" name="text2" id="text2" size="50" value="Submit" />
                    <input class="open" type="button" value="Submit" name="submit2" />
                </fieldset>
            </form>
        </div><!--/div-->

        <h3 id="section3"><a href="#">Section 3</a></h3>
        <div>
            <form id="form3" action="form.php" method="post">
                <fieldset class="inside">
                    <input type="text" name="text3" id="text3" size="50" value="Submit" />
                    <input class="open" type="button" value="Submit" name="submit3" />
                </fieldset>
            </form>
        </div><!--/div-->

**Nevermind. I figured it out. **

I did it like this:

$("#accordion").accordion({
        header:'h3',
        active: '#section1',
        autoheight: false,
        clearstyle: true,
}).bind("change.ui-accordion", function(event,ui) {
    $("#text1").focus();
});

I've got an accordion all set up, and each div has a form within it. I'm just trying to figure out how to set the focus on an input field depending on which one is open...

/* accordion */
$("#accordion").accordion({
        header:'h3',
        active: '#section1',
        autoheight: false,
        clearstyle: true
});

Basically, I want to set the cursor in the first input field for whichever section is open. The actual forms are much bigger, so I condensed it enormously...

    <div id="accordion">
        <h3 id="section1"><a href="#">Section 1/a></h3>
        <div>
            <form id="form1" action="form.php" method="post">
                <fieldset class="inside">
                    <input type="text" name="text1" id="text1" size="50" value="Default text" />
                    <input class="open" type="button" value="Submit" name="submit1" />
                </fieldset>
            </form>
        </div><!--/div-->

        <h3 id="section2"><a href="#">Section 2</a></h3>
        <div>
            <form id="form2" action="form.php" method="post">
                <fieldset class="inside">
                    <input type="text" name="text2" id="text2" size="50" value="Submit" />
                    <input class="open" type="button" value="Submit" name="submit2" />
                </fieldset>
            </form>
        </div><!--/div-->

        <h3 id="section3"><a href="#">Section 3</a></h3>
        <div>
            <form id="form3" action="form.php" method="post">
                <fieldset class="inside">
                    <input type="text" name="text3" id="text3" size="50" value="Submit" />
                    <input class="open" type="button" value="Submit" name="submit3" />
                </fieldset>
            </form>
        </div><!--/div-->

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹊巢 2024-08-31 04:55:40
$("#accordion").accordion({ header:'h3', active: '#section1', autoheight: false, clearstyle: true, }).bind("change.ui-accordion", function(event,ui) { $("#text1").focus(); });

这不适用于其他子手风琴。当您对文本框的 ID 进行硬编码以获取焦点时。

可能可以在change.ui-accordion 事件中加入一些内容,但我对此不太熟悉。你可以使用这样的东西:

    $(document).ready(function() {
        $("div#accordion > h3 > a").click(function(e) { // when we click a link

            e.preventDefault(); // prevent the click from bubbling

            var parenth3 = $(this).parent(); // find the parent h3 the sender is in

            //this selector then finds the first textbox that is in the div adjacent to the parent h3.
            $("#" + parenth3[0].id + " + div > form > fieldset.inside > input[type=text]:first").focus();

        });
    });

这对我来说感觉很老套。

编辑:另请注意,第 1 部分的锚标记未正确关闭。

$("#accordion").accordion({ header:'h3', active: '#section1', autoheight: false, clearstyle: true, }).bind("change.ui-accordion", function(event,ui) { $("#text1").focus(); });

That's not going to work for the other sub-accordions. As you're hard-coding the ID of the textbox to focus.

It's probably possible to hook into something on that change.ui-accordion event but I'm not terribly familiar with it. You could use something like this:

    $(document).ready(function() {
        $("div#accordion > h3 > a").click(function(e) { // when we click a link

            e.preventDefault(); // prevent the click from bubbling

            var parenth3 = $(this).parent(); // find the parent h3 the sender is in

            //this selector then finds the first textbox that is in the div adjacent to the parent h3.
            $("#" + parenth3[0].id + " + div > form > fieldset.inside > input[type=text]:first").focus();

        });
    });

This feels pretty hacky to me.

Edit: Also note that your anchor tag for Section 1 is not closed properly.

乄_柒ぐ汐 2024-08-31 04:55:40

上面提到的两种方法(.bind("change.ui-accordion"...activate: function( event, ui ) {...) 如果有的话就可以工作只有一个需要关注的输入。我有 3 个文本输入,用户需要能够点击它们来解决这个问题:

    $(":input").click(function() {
      $(this).focus();
    });

当然,“:input”可以更改为您需要的任何内容。

The two methods mentioned above (.bind("change.ui-accordion"... and activate: function( event, ui ) {...) work if there is only one input that needs to be focused. I had 3 text inputs that the user needed to be able to click through. To fix this I used:

    $(":input").click(function() {
      $(this).focus();
    });

Of course ":input" can be changed to whatever you need.

记忆消瘦 2024-08-31 04:55:40

最好使用像这样的事件中内置的手风琴,所以

    $(".accordion").accordion({
        beforeActivate: function()  {},
        activate: function( event, ui ) {
            $(ui.newPanel).find('input')[0].focus()
         }
    });

姜是最好的

it's better to use the accordion built in events like so

    $(".accordion").accordion({
        beforeActivate: function()  {},
        activate: function( event, ui ) {
            $(ui.newPanel).find('input')[0].focus()
         }
    });

gingers are the best

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文