【问题标题】:How to get value of hidden field in jquery each loop? [duplicate]如何在jquery每个循环中获取隐藏字段的值? [复制]
【发布时间】:2018-08-29 12:08:15
【问题描述】:

我正在遍历 Div 并在该 div 中将编码数组作为值分配给隐藏字段,并希望在每个循环中获取该隐藏字段的值但未定义

var port_ofAir = null; $(".sublocation_div").find('.sublocation').each(function(index,value1){
    port_ofAir = $(this).find(".port_arr").value;

  });
  console.log(port_ofAir)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" name="arr_port" class="port_arr" id="arr-port" value="[{"id":34,"client_id":"2"}]">

我想在 jquery 函数中得到那个数组

请给个建议

【问题讨论】:

  • 尝试`.val()'。不确定,但试试吧
  • 您可以在 jQuery 中使用 .val() 来获取表单元素的值。尽管那个特定的表单元素看起来像是损坏的 HTML 语法。 value 只有"[{",其他的都在value 属性之外。
  • 你能告诉我们div元素的类sublocation_div吗?

标签: javascript jquery


【解决方案1】:

在 jQuery 中你需要使用 .val() 而不是 .value:

port_ofAir = $(this).find(".port_arr").val();

【讨论】:

  • 仍然不被定义
  • 你确定它是你循环遍历的 .sublocation 元素的后代吗?
【解决方案2】:

试试这个:你需要使用.val(),而不是value。此外,您可以使用 input[type=hidden] 为隐藏字段添加特定的 jQuery 选择器

$(".sublocation_div").find('.sublocation').each(function(index,value1){
    var port_ofAir = $(this).find("input[type=hidden].port_arr").val();
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 2013-09-06
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多