第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

HTML所有按鈕不起作用,只有一個(gè)按鈕起作用

HTML所有按鈕不起作用,只有一個(gè)按鈕起作用

Go
慕標(biāo)琳琳 2022-06-01 10:05:47
我正在使用 go Templete 在 HTML 模板中設(shè)置值,但在這里。我正在創(chuàng)建許多<button id="exec" cid="{{.Id}}">Show</button>使用循環(huán),但只有第一個(gè)按鈕單擊功能有效,其他無(wú)效。只發(fā)生了第一個(gè)按鈕 ajax 調(diào)用。  <body >    <div style="width: 100%;">    <div style="height:700px;overflow:auto;float:left;width: 50%">    <table>        <tr>          <th>Country Name</th>          <th>View</th>        </tr>        {{ range .CountryData }}                <tr>                      <td>{{ .Name }}</td>                      <td><button id="exec" cid="{{.Id}}">Show</button></td>                </tr>        {{ end }}     </table>    </div>    <div id="response" style="height:700px;overflow:auto;float:left;width: 50%"></div></div>  </body>    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>    <script type="text/javascript">        $(document).ready(function() {            $("#exec").on("click", function() {                var urlData = "/country?cid=" + $(this).attr("cid");                $.ajax({                    url: urlData,                    method: "GET",                    success: function(data) {                        $("#response").html(data);                    },                });            });        });    </script>
查看完整描述

1 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

html 文檔中元素的id屬性應(yīng)該是唯一的,并且您可以使用$("#exec")僅選擇具有該 id 的第一個(gè)元素來(lái)選擇元素。

您可以移動(dòng)到類選擇器,或使用多個(gè) id 并通過(guò)其他選擇器進(jìn)行選擇:


<body >

    <div style="width: 100%;">

    <div style="height:700px;overflow:auto;float:left;width: 50%">

    <table>

        <tr>

          <th>Country Name</th>

          <th>View</th>

        </tr>

        {{ range .CountryData }}

                <tr>

                      <td>{{ .Name }}</td>

                      <td><button class="exec" cid="{{.Id}}">Show</button></td>

                </tr>

        {{ end }} 

    </table>

    </div>

    <div id="response" style="height:700px;overflow:auto;float:left;width: 50%"></div>

</div>

  </body>

    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

    <script type="text/javascript">

        $(document).ready(function() {

            $("button.exec").on("click", function() {

                var urlData = "/country?cid=" + $(this).attr("cid");

                $.ajax({

                    url: urlData,

                    method: "GET",

                    success: function(data) {

                        $("#response").html(data);

                    },

                });

            });

        });

    </script>

如果無(wú)法更改id道具,您可以添加一些其他類并使用其他選擇器。例如 - 您可以將 id 添加到表中,然后選擇該特定表中的所有按鈕:


<table id="table-with-buttons">

    ...

    {{ range .CountryData }}

            <tr>

                  <td>{{ .Name }}</td>

                  <td><button id="exec" cid="{{.Id}}">Show</button></td>

            </tr>

    {{ end }} 

</table>

在您的代碼中,您可以使用以下方法選擇所有按鈕:


$("#table-with-buttons button").on("click", function() {

    ....

});


查看完整回答
反對(duì) 回復(fù) 2022-06-01
  • 1 回答
  • 0 關(guān)注
  • 87 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)