jquery:在父div id上更改输入val basend

时间:2017-01-09 23:39:07

标签: javascript jquery html

我有一些阻力和可排序的ul .... 现在我需要设置隐藏输入字段" area []"值 ul属性位置

的相同值
<ul id='sortlistThree' location='1' class='connectMe sort'>
  <li>
    <form class='sort'>
        <input type='hidden' name='area[]' class='dienstplan' data='getform' value='' />
    </form>
  </li>
  <li>
    <form class='sort'>
        <input type='hidden' name='area[]' class='dienstplan' data='getform' value='' />
    </form>
  </li>
</ul>

<ul id='sortlistThree' location='2' class='connectMe sort'>
  <li>
    <form class='sort'>
        <input type='hidden' name='area[]' class='dienstplan' data='getform' value='' />
    </form>
  </li>
</ul>

<ul id='sortlistThree' location='3' class='connectMe sort'>
  <li>
    <form class='sort'>
        <input type='hidden' name='area[]' class='dienstplan' data='getform' value='' />
    </form>
  </li>
</ul>

1 个答案:

答案 0 :(得分:1)

使用JQuery你可以这样做,我删除了hidden,这样你就可以看到发生了什么。

&#13;
&#13;
$('input[name="area[]"]').each(function(){
     $(this).attr('value',$(this).closest('ul').attr('location'));
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id='sortlistThree' location='1' class='connectMe sort'>
  <li>
    <form class='sort'>
        <input name='area[]' class='dienstplan' data='getform' value='1' />
    </form>
  </li>
</ul>

<ul id='sortlistThree' location='2' class='connectMe sort'>
  <li>
    <form class='sort'>
        <input  name='area[]' class='dienstplan' data='getform' value='1' />
    </form>
  </li>
</ul>

<ul id='sortlistThree' location='3' class='connectMe sort'>
  <li>
    <form class='sort'>
        <input  name='area[]' class='dienstplan' data='getform' value='1' />
    </form>
  </li>
</ul>
&#13;
&#13;
&#13;