我想用jQuery显示我的div值。我有两个div内容值,第一个值为subcategory value1
,第二个值为subcategory value2
。所以当我点击按钮时,我想显示它们的相应值。
例如,如果我点击第二个div按钮,我想显示值subcategory value2
。
下面我提到了您的示例代码:
<div class='row'>
<div class='form-group col-md-10'>
<label>Sub Category Name:</label>
<br />
<input type='text' name='Sub_category1' id='Sub_category_id1' class='form-control' value="subcategory value1">
</div>
<div class='form-group col-md-2'>
<br />
<button type='button' id='showvalue' class='deleteupdateContact btn btn btn-danger btn-xs'>SHOW</button>
</div>
</div>
<div class='row'>
<div class='form-group col-md-10'>
<label>Sub Category Name:</label>
<br />
<input type='text' name='Sub_category2' id='Sub_category_id2' class='form-control' value="subcategory value2">
</div>
<div class='form-group col-md-2'>
<br />
<button type='button' id='showvalue' class='deleteupdateContact btn btn btn-danger btn-xs'>SHOW</button>
</div>
</div>
请查看此代码..为我提供最佳解决方案..谢谢你提前到每一个..
答案 0 :(得分:0)
不要在任何情况下对HTML的多个元素使用相同的id。
$('.showvalue').click(function(){
alert($(this).parent().parent('.row').find('input[type="text"]').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='row'>
<div class='form-group col-md-10'>
<label>Sub Category Name:</label>
<br />
<input type='text' name='Sub_category1' id='Sub_category_id1' class='form-control' value="subcategory value1">
</div>
<div class='form-group col-md-2'>
<br />
<button type='button' class='deleteupdateContact btn btn btn-danger btn-xs showvalue'>SHOW</button><!-- added showvalue as button class-->
</div>
</div>
<div class='row'>
<div class='form-group col-md-10'>
<label>Sub Category Name:</label>
<br />
<input type='text' name='Sub_category1' id='Sub_category_id1' class='form-control' value="subcategory value2">
</div>
<div class='form-group col-md-2'>
<br />
<button type='button' class='deleteupdateContact btn btn btn-danger btn-xs showvalue'>SHOW</button><!-- added showvalue as button class-->
</div>
</div>