英文:
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 -->
$('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);
});
});
<!-- language: lang-css -->
.d-custom-none{ display: none;}
.select-response-type-box{ border: 1px solid #ccc; padding: 10px;}
<!-- language: lang-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>
<!-- end snippet -->
Answer will appreciated!
答案1
得分: 1
-
为什么要使用 HTML 表格?在 div 和 tr 之间获取
index
很困难。 -
最重要的是 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;
$('body').on('click', '.select-response-type-btn', function () {
clickIndex = $(this).parent().parent().parent().index();
console.log(clickIndex);
var thisClick = $(this);
$('.select-response-type-box').show();
$('.response-menu-item').click(function () {
var thisMunuItemHtml = $(this).html();
$('.select-response-type-box').hide();
$('.type-selector').find('.response-menu-item').eq(clickIndex).html(thisMunuItemHtml);
//$(thisClick).prev('.response-menu-item').html(thisMunuItemHtml);
});
});
-
Why you use HTML Table ?? It's hard to get
index
between div and tr. -
The most important is jquery selector
.select-response-type-btn
return wrong position. you must print click index likeclickIndex = $(this).parent().parent().parent().index();
if clickIndex return correct index then your example will working.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论