通过div标签id获取隐藏的值?

时间:2017-12-29 16:10:43

标签: javascript jquery

<div id = "abc1">
<input type = "hidden" id= "aabc_0_the" value = 314/>
<input type = "hidden" id= "aabc_1_the" value = 315/>
</div><div id = "abc2">
<input type = "hidden" id= "aabc_0_the" value = 316/>
<input type = "hidden" id= "aabc_1_the" value = 317/>
</div>

现在我需要使用JavaScript获取输入标记中的值 因为我需要通过ajax调用将值发送到控制器。我在下面尝试了未定义的内容。

document.body.querySelectorAll('#abc1 > aabc_0_the').value

每个div都是局部视图

3 个答案:

答案 0 :(得分:1)

ID是唯一的,因此您应该更改ID或使用类。

<div id = "abc1">
    <input type = "hidden" id= "aabc_0_the" value = "314"/>
</div>
<div id = "abc2">
    <input type = "hidden" id= "aabc_1_the" value = "315"/>
</div>

使用javascript:

var value=document.getElementById('aabc_0_the').value;
var value1=document.getElementById('aabc_1_the').value;

在jQuery中:

var value = $('#aabc_0_the').val();
var value1 = $('#aabc_1_the').val();

答案 1 :(得分:0)

您无法复制ID。

在JS中

var value=document.body.getElementById('aabc_0_the').value();

在JQuery中

    var value=$('#aabc_0_the').val();


<div id = "abc1">
<input type = "hidden" class= "aabc_0_the" value = 314/>
</div><div id = "abc2">
<input type = "hidden" class= "aabc_0_the" value = 315/>
</div>


var v1=$('#abc1 .aabc_0_the:eq(0)').val();
var v2=$('#abc1 .aabc_0_the:eq(1)').val();

or

var v1=$('#abc1 .aabc_0_the').first().val();
var v1=$('#abc1 .aabc_0_the').last().val();

or

var v1=$('#abc1 .aabc_0_the:first').val();
var v1=$('#abc1 .aabc_0_the:last').val();

答案 2 :(得分:0)

您也可以这样做,

 $(document).ready(function () {
    var value_one = $('#abc1').find('input[type="hidden"]').val();
    var value_two = $('#abc2').find('input[type="hidden"]').val();
    console.log(value_one, value_two);
});
相关问题