简体   繁体   English

jQuery console.log和名称附加不起作用

[英]Jquery console.log and name attach not working

I'm using a plugin to give my <select> elements images loaded by jquery. 我正在使用一个插件来给jquery加载的<select>元素图像。 The thing is, the names are not transferring to the new code's <input> elements, and thus cannot submit the form surrounding it. 问题是,名称没有转移到新代码的<input>元素上,因此无法提交围绕它的表单。

The plugin turns this code: 插件会显示以下代码:

<select id="itemselect1" name="item1">
    <option>1</option>
    <option selected>2</option>
    <option>3</option>
</select>
<select id="itemselect2" name="item3">
    <option>A</option>
    <option>B</option>
    <option selected>C</option>
</select>

Into: (I simplified it a bit) 进入:(我简化了一点)

<div id="itemselect1" name="item1" class="dd-container">
    <div class="dd-select">
        <input class="dd-selected-value" value="2" type="hidden">
    </div>
</div>

<div id="itemselect2" name="item2" class="dd-container">
    <div class="dd-select">
        <input class="dd-selected-value" value="C" type="hidden">
    </div>
</div>  // the list continues

So I tried this approach to transfer the names over: 所以我尝试了这种方法来转移名称:

var list = {
  "itemselect1": "item1",
  "itemselect2": "item2",
  "itemselect3": "item3",
  "itemselect4": "item4",
  "itemselect5": "item5",
  "itemselect6": "item6"
};
$.each( list, function( key, value ) {
  $("#"+ key).children().children("input.dd-selected-value").attr("name", value );
  console.log(key);
  console.log(value);       
  console.log($("div#"+ key).attr("name"));   // To diagnose why it isnt working
});

And the console just reads: 控制台显示为:

itemselect1 (index):32
item1 (index):33
undefined (index):34
itemselect2 (index):32
item2 (index):33
undefined (index):34

Regardless if I use the plugin or now, the names aren't getting attached somehow. 无论我现在还是现在使用插件,都不会以任何方式附加名称。 Please help, Thanks. 请帮忙,谢谢。

use find () in jquery. 在jQuery中使用find ()。 you assigned name attribute to input but you print and check the parent div name so it gives undefined. 您为输入分配了名称属性,但是您打印并检查了父div名称,因此未定义。

$.each( list, function( key, value ) {
  $("#"+ key).find(".dd-select input.dd-selected-value").attr("name", value);  
  console.log($("#"+ key).find("input.dd-selected-value").attr("name"));   
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM