单击时更改输入字段的文本

时间:2016-08-10 16:12:08

标签: javascript jquery html forms input

目的

  • 在我的if-else语句中.player有类 picked.is-inactive输入字段的文本应为空白,但现在它是最后点击播放器的名称
  • 现在,当选择一个玩家时,所有20个输入字段都有 文本更改为最后点击的播放器。 然而,名称 picked.is-active的第一个玩家应该被放入第一个玩家 输入字段#p1等等,直到选中所有20个玩家并填写20个输入字段。

scripts.js中

// Every time a player is clicked, note the name of the player
$(".player").on("click", function(){

    // Get the name of that player, only if picked.is-active
    // Put the text of that player into the appropriate input field

    if ($(this).find("picked.is-active")) {
        var playerName = $(this).find(".player__name").html();
        $("input").val(playerName);
        console.log(playerName);
    } else {
        $("input").val("")
    }
});

index.html(表格)

    <form id="form">
        <input type="text" name="p1"  id="p1" required>
        <input type="text" name="p2"  id="p2" required>
        <input type="text" name="p3"  id="p3" required>
        <input type="text" name="p4"  id="p4" required>
        <input type="text" name="p5"  id="p5" required>
        <input type="text" name="p6"  id="p6" required>
        <input type="text" name="p7"  id="p7" required>
        <input type="text" name="p8"  id="p8" required>
        <input type="text" name="p9"  id="p9" required>
        <input type="text" name="p10" id="p10" required>
        <input type="text" name="p11" id="p11" required>
        <input type="text" name="p12" id="p12" required>
        <input type="text" name="p13" id="p13" required>
        <input type="text" name="p14" id="p14" required>
        <input type="text" name="p15" id="p15" required>
        <input type="text" name="p16" id="p16" required>
        <input type="text" name="p17" id="p17" required>
        <input type="text" name="p18" id="p18" required>
        <input type="text" name="p19" id="p19" required>
        <input type="text" name="p20" id="p20" required>
        <button type="submit">
            Submit your vote
        </button>

index.html(播放器)

<div class="player player--goalie year--1970">
                    <div class="tooltip tooltip--tall">
                        <p class="tooltip__name">Glen Hanlon</p>
                        <p class="tooltip__hometown"><span>Hometown:</span> Brandon, Man.</p>
                        <p class="tooltip__years"><span>Years Played:</span> 1974-1977</p>
                        <div class="tooltip__stats--inline">
                            <div class="stats__group stats--games">
                                <p class="stats__header">GP</p>
                                <p class="stats__number--games">172</p>
                            </div>

                            <div class="stats__group stats--goalsag">
                                <p class="stats__header">GA</p>
                                <p class="stats__number">4.22</p>
                                <p class="stats__number">3.99</p>
                                <p class="stats__number stats__number--goalsag">3.09</p>
                            </div>

                            <div class="stats__group stats--savep">
                                <p class="stats__header">SAV%</p>
                                <p class="stats__number">.892</p>
                                <p class="stats__number">.891</p>
                                <p class="stats__number stats__number--savep">.906</p>
                            </div>

                            <div class="stats__group stats--shutouts">
                                <p class="stats__header">SO</p>
                                <p class="stats__number">0</p>
                                <p class="stats__number stats__number--shutouts">4</p>
                                <p class="stats__number">4</p>
                            </div>
                        </div> <!-- tooltip__stats--inline -->
                    </div> <!-- tooltip -->
                    <div class="player__headshot player--hanlon">
                        <div class="picked is-inactive"><i class="fa fa-star" aria-hidden="true"></i></div>
                    </div>
                    <p class="player__name" value="Glen Hanlon">Glen Hanlon</p>
                    <p class="player__position">Goalie</p>
                </div>

1 个答案:

答案 0 :(得分:1)

您需要告诉具体文本框应该更新。

protected $redirectTo = '/private_dashboard';
protected $redirectAfterLogout = '/public_homepage';

<强>解释

每次点击

,使用// Every time a player is clicked, note the name of the player $(".player").on("click", function(){ var playerNames = []; $("input:text").each(function(i, t) { playerNames.push(t.value) }); if ($(this).find("picked.is-active")) { var playerName = $(this).find(".player__name").html(); var index = playerNames.indexOf(playerName); if(index == -1) // add player $("input:text:eq(" + playerNames.indexOf("") + ")").val(playerName); else // remove player $("input:text:eq(" + index + ")").val(""); } else { $("input").val(""); } }); 循环将所有文本框值转换为arry playerNames

然后检查$.each数组中是否已存在的有效(或已点击)playerName

如果不存在(即playerNames),请将其添加到下一个空文本框

index == -1带来下一个空文本框的索引

否则将其从文本框中删除。

希望这有帮助!