Jquery将name属性传递给child

时间:2014-09-21 08:18:54

标签: javascript jquery html

我试图将元素的name属性转移2代。原因是,我使用jquery plugin<select>元素提供了图像下拉列表。它使用jquery转换这样的东西:

<select id="fruitselect" name="fruit">
    <option data-description="It's red" >Apple</option>
    <option data-description="It's yellow" >Banana</option>
    <option data-description="It's .... orange" >Orange</option>
</select>

要:

<div id="fruitselect" name="fruit" class="dd-container">
    <div class="dd-select">
        <input class="dd-selected-value" type="hidden" value="Apple">  // If I had picked Apple
        <a class="dd-selected">
            <img class="dd-selected-image" src="">  //I have images disabled for simplicity
            <label class="dd-selected-text">Apple</label>
            <small class="dd-selected-description"> It's red </small>
        </a>        
    </div>
    <ul class="dd-options">
        <li>
            <a class="dd-option dd-option-selected">
                <input class="dd-option-value" type="hidden" value="Apple" name="fruit"> // I was able to get the name here with jquery
                <img class="dd-option-image" src="">
                <label class="dd-option-text">Apple</label>
                <small class="dd-option-description"> It's red </small>
            </a>
        </li>
        <li>      // another li for each other <option>
            <a class="dd-option">
            ...
            </a>
        </li>
        <li> 
            <a class="dd-option">
            ...
            </a>
        </li>
    </ul>
</div>

该插件效果很好,图像显示效果很好。问题是我无法通过PHP提交form,因为name属性未被隐藏<input>元素。

如何从第一段代码中的<select>元素到第二段代码中的<input class="dd-selected-value">标记获取名称。我会执行$this.parentUntil("#fruitselect").attr("name")这样的插件转换<select>代码吗?

要考虑的另一件事是,从2个父母那里得到name是否合理,或者从name获得<input class="dd-option-value">

我是jquery的新手,非常感谢!

1 个答案:

答案 0 :(得分:0)

应用插件后,您只需阅读&#34;名称&#34; #fruitselect中的属性并将其注入输入标记:

var n = $("#fruitselect").attr("name");

$("input.dd-selected-value").attr("name", n );

或者在一行中:

$("input.dd-selected-value").attr("name", $("#fruitselect").attr("name"));