[英]How to get the ID of the Current element clicked
I want to get the Value of the Current Element to be clicked. 我想获得要单击的当前元素的值。 I have a list of Checkboxes and selection of each I want to get the ID of it which is hidden.
我有一个复选框列表,每个复选框的选择我都想获取隐藏的ID。 My Code goes as follows:
我的代码如下:
$("#ModelListView").on("click", ".ModelCheckBox", function (element) {
var AnalysisID = $("#AnalysisID").val();
var ModelID = '';
});
HTML: HTML:
<div id="ModelListView"></div>
<script type="text/x-kendo-template" id="Modeltemplate">
<div class="section group fr">
<div class="col span_2_of_12">
#if(ACTIVE_MODELS_COUNT > 0){# <input class="ModelCheckBox" type="checkbox" checked/>#} else {# <input class="ModelCheckBox" type="checkbox" unchecked/> #}#
</div>
<div class="col span_4_of_12"><label>#:MODEL#</label></div>
<input id="Model_ID" type="hidden" value=#:MODEL_ID#/>
</div>
</script>
I want to get the Value of Model_ID that is kept hidden. 我想获取隐藏的Model_ID的值。
You can use $(this)
and get the closest. 您可以使用
$(this)
获得最接近的值。
$(this)
will be the element that's clicked. $(this)
将是被单击的元素。
.closest('.section.group')
will return the " section group
"-div. .closest('.section.group')
将返回“ section group
” -div。 You might want to use #ModelListView
instead of . 您可能要使用
#ModelListView
而不是。 section.group
. section.group
。
.find('#Model_ID').val()
will return the value of the hidden field. .find('#Model_ID').val()
将返回隐藏字段的值。
$("#ModelListView").on("click", ".ModelCheckBox", function (element) { var AnalysisID = $("#AnalysisID").val(); var ModelID = $(this).closest('.section.group').find('#Model_ID').val(); alert(ModelID); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="ModelListView"> <div class="section group fr"> <div class="col span_2_of_12"> <input class="ModelCheckBox" type="checkbox" checked/> </div> <div class="col span_4_of_12"><label>Label</label></div> <input id="Model_ID" type="hidden" value="someValue"/> </div> </div>
Sidenote: be aware of using an ID in a template. 旁注:请注意在模板中使用ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.