这是我的html片段,输入是标签和内容。
=============================================== ========================
<div class="row">
<div class="col-md-5">
<div class="form-group ">
<label>Nome del campo</label>
<div class="input">
<input class="form-control" name="label" value="c">
<input type="hidden" class="form-control" name="id_anagrafica" value="184">
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group ">
<label>Valore</label>
<div class="input">
<input class="form-control" name="content" value="d">
</div>
</div>
</div>
<div class="col-md-2" style="margin-top: 16px">
<button type="button" class="btn btn-danger" onclick="getInfo(event)">GET</button>
</div>
</div>
答案 0 :(得分:1)
$(".btn-danger").click(function() {
console.log("Label " + $(this).parent('div').prev('div').find('label').text())
console.log("input " + $(this).parent('div').prev('div').find('input').val())
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-5">
<div class="form-group ">
<label>Nome del campo</label>
<div class="input">
<input class="form-control" name="label" value="c">
<input type="hidden" class="form-control" name="id_anagrafica" value="184">
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group ">
<label>Valore</label>
<div class="input">
<input class="form-control" name="content" value="d">
</div>
</div>
</div>
<div class="col-md-2" style="margin-top: 16px">
<button type="button" class="btn btn-danger">GET</button>
</div>
</div>
&#13;
答案 1 :(得分:0)
尝试使用jquery。使用getInfo(event,this)
previous div.col-md-2
上有输入,因此请与prev()
find()
采取特定元素
function getInfo(e,that){
console.log($(that).closest('.col-md-2').prev('.col-md-2').find('.form-control').val())
console.log($(that).closest('.col-md-2').prev('.col-md-2').find('label').text())
console.log($(that).closest('.col-md-2').prev('.col-md-2').find('.form-control').attr('name'))
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-5">
<div class="form-group ">
<label>Nome del campo</label>
<div class="input">
<input class="form-control" name="label" value="c">
<input type="hidden" class="form-control" name="id_anagrafica" value="184">
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group ">
<label>Valore</label>
<div class="input">
<input class="form-control" name="content" value="d">
</div>
</div>
</div>
<div class="col-md-2" style="margin-top: 16px">
<button type="button" class="btn btn-danger" onclick="getInfo(event,this)">GET</button>
</div>
</div>
&#13;
答案 2 :(得分:0)
如果你需要内容的价值,你应该使用closest()
使用带有按钮和输入的div,然后使用find()
获取名称为content
<的输入/ p>
function getInfo(el) {
var content = $(el).closest('.row').find('input[name="content"]').val()
console.log(content);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-5">
<div class="form-group ">
<label>Nome del campo</label>
<div class="input">
<input class="form-control" name="label" value="c">
<input type="hidden" class="form-control" name="id_anagrafica" value="184">
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group ">
<label>Valore</label>
<div class="input">
<input class="form-control" name="content" value="d">
</div>
</div>
</div>
<div class="col-md-2" style="margin-top: 16px">
<button type="button" class="btn btn-danger" onclick="getInfo(this)">GET</button>
</div>
</div>
&#13;
答案 3 :(得分:0)
Add input type text in input tag
$(".form-group").each(function(){
var formVal = $(this).find(".form-control[type='text']").val();
var formHiddenVal = $(this).find(".form-control[type='hidden']").val();
var labelTxt = $(this).children("label").text();
});