为什么在点击时数值相同?

huangapple go评论91阅读模式
英文:

Why value is coming same on click?

问题

我有两个Table TR,其中定义了带有fa-chevron-down Font Awesome图标的控件类型。

我想要:
当我点击fa-chevron-down Font Awesome图标时,打开一个控件类型,现在有两种类型(输入控件电子邮件控件),但它可能有多个。

当我点击控件类型时,它会更改Table TR中给定的主要值。

第一次点击fa-chevron-down Font Awesome图标时它可以正常工作,但是当我点击第二次fa-chevron-down时,两个Table TR的值都会更改,我为什么无法理解。

我的代码:

<!-- 开始代码段 -->
<!-- JavaScript代码 -->
$('body').on('click', '.select-response-type-btn', function (e) {
    var thisClick = $(this);
    $('.select-response-type-box').show();
    $('.response-menu-item').click(function () {
        var thisMunuItemHtml = $(this).html();
        $('.select-response-type-box').hide();
        $(thisClick).prev('.response-menu-item').html(thisMunuItemHtml);
    });
});

<!-- CSS代码 -->
.d-custom-none { display: none; }
.select-response-type-box { border: 1px solid #ccc; padding: 10px; }

<!-- HTML代码 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/js/all.js" integrity="sha256-2JRzNxMJiS0aHOJjG+liqsEOuBb6++9cY4dSOyiijX4=" crossorigin="anonymous"></script>

<table class="table table-bordered">
    <tbody>
        <tr>
            <td>
                <div class="type-selector">
                    <div class="response-menu-item">
                        <div class="selected-icon-circle icon-text-light-green">
                            <i class="far fa-calendar-alt"></i>
                        </div>
                        <span>Date & Time</span>
                    </div>
                    <i class="fas fa-chevron-down chevron-custom select-response-type-btn" id="type_btn1"></i>
                </div>
            </td>
        </tr>

        <tr>
            <td>
                <div class="type-selector">
                    <div class="response-menu-item">
                        <div class="selected-icon-circle icon-text-light-green">
                            <i class="far fa-calendar-alt"></i>
                        </div>
                        <span>Date & Time control</span>
                    </div>
                    <i class="fas fa-chevron-down chevron-custom select-response-type-btn" id="type_btn2"></i>
                </div>
            </td>
        </tr>

    </tbody>
</table>

<div class="select-response-type-box d-custom-none">
    <div class="response-menu-item">input control</div>
    <div class="response-menu-item">email control</div>
</div>
<!-- 结束代码段 -->

将代码段添加到你的项目中,它应该能够正常工作。

英文:

I have two Table TR there is define control types with fa-chevron-down font awesome icon.

I want:
When i click on fa-chevron-down font awesome icon then open a control types now its two types (input control and email control) but it may be multiple.

When i click on control types then it is change the main value which are given in Table TR.

It is working fine for first fa-chevron-down font awesome icon click when i click on second fa-chevron-down then both Table TR value changed, why i'm not able to understand.

My Code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 $(&#39;body&#39;).on(&#39;click&#39;, &#39;.select-response-type-btn&#39;, function (e) {
var thisClick = $(this);
$(&#39;.select-response-type-box&#39;).show();
$(&#39;.response-menu-item&#39;).click(function () {
var thisMunuItemHtml = $(this).html();
$(&#39;.select-response-type-box&#39;).hide();
$(thisClick).prev(&#39;.response-menu-item&#39;).html(thisMunuItemHtml);
});
});

<!-- language: lang-css -->

        .d-custom-none{ display: none;}
.select-response-type-box{ border: 1px solid #ccc; padding: 10px;}

<!-- language: lang-html -->

  &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css&quot;&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/js/all.js&quot;
integrity=&quot;sha256-2JRzNxMJiS0aHOJjG+liqsEOuBb6++9cY4dSOyiijX4=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;table class=&quot;table table-bordered&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div class=&quot;type-selector&quot;&gt;
&lt;div class=&quot;response-menu-item&quot;&gt;
&lt;div class=&quot;selected-icon-circle icon-text-light-green&quot;&gt;
&lt;i class=&quot;far fa-calendar-alt&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;span&gt;Date &amp; Time&lt;/span&gt;
&lt;/div&gt;
&lt;i class=&quot;fas fa-chevron-down chevron-custom select-response-type-btn&quot; id=&quot;type_btn1&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div class=&quot;type-selector&quot;&gt;
&lt;div class=&quot;response-menu-item&quot;&gt;
&lt;div class=&quot;selected-icon-circle icon-text-light-green&quot;&gt;
&lt;i class=&quot;far fa-calendar-alt&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;span&gt;Date &amp; Time control&lt;/span&gt;
&lt;/div&gt;
&lt;i class=&quot;fas fa-chevron-down chevron-custom select-response-type-btn&quot; id=&quot;type_btn2&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;div class=&quot;select-response-type-box d-custom-none&quot;&gt;
&lt;div class=&quot;response-menu-item&quot;&gt;input control&lt;/div&gt;
&lt;div class=&quot;response-menu-item&quot;&gt;email control&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Answer will appreciated!

答案1

得分: 1

  1. 为什么要使用 HTML 表格?在 div 和 tr 之间获取 index 很困难。

  2. 最重要的是 jQuery 选择器 .select-response-type-btn 返回错误的位置。你必须打印点击的索引,像这样:clickIndex = $(this).parent().parent().parent().index(); 如果 clickIndex 返回正确的索引,那么你的示例将正常工作。

英文:

I spent a lot of time made an working example.
jsfiddle

var clickIndex = 0;
$(&#39;body&#39;).on(&#39;click&#39;, &#39;.select-response-type-btn&#39;, function () {
clickIndex = $(this).parent().parent().parent().index();
console.log(clickIndex);
var thisClick = $(this);
$(&#39;.select-response-type-box&#39;).show();
$(&#39;.response-menu-item&#39;).click(function () {
var thisMunuItemHtml = $(this).html();
$(&#39;.select-response-type-box&#39;).hide();
$(&#39;.type-selector&#39;).find(&#39;.response-menu-item&#39;).eq(clickIndex).html(thisMunuItemHtml);
//$(thisClick).prev(&#39;.response-menu-item&#39;).html(thisMunuItemHtml);
});
});
  1. Why you use HTML Table ?? It's hard to get index between div and tr.

  2. The most important is jquery selector .select-response-type-btn return wrong position. you must print click index like clickIndex = $(this).parent().parent().parent().index(); if clickIndex return correct index then your example will working.

huangapple
  • 本文由 发表于 2020年1月3日 13:59:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定