问题描述
这是我的XHTML
我试图获得使用jQuery发送到PHP脚本的元素的索引。 p>
< form action =/ accreditation / questions /method =postid =questions-form>
< fieldset id =question-0>
<图例>问题< / legend>
< h3>草是什么颜色?< / h3>
< ul>
< li>
< input type =radioname =answer [0]value =0id =radio-0-0/>
< label for =radio-0-0>绿色< / label>
< / li>
< li>
< input type =radioname =answer [0]value =1id =radio-0-1/>
< label for =radio-0-1>红色< / label>
< / li>
< li>
< input type =radioname =answer [0]value =2id =radio-0-2/>
< label for =radio-0-2> Orange< / label>
< / li>
< / ul>
< / fieldset>
< fieldset id =问题-1>
<图例>问题< / legend>
< h3> alex多少岁< / h3>
< ul>
< li>
< input type =radioname =answer [1]value =0id =radio-1-0/>
< label for =radio-1-0> 21< / label>
< / li>
< li>
< input type =radioname =answer [1]value =1id =radio-1-1/>
< label for =radio-1-1> 11< / label>
< / li>
< li>
< input type =radioname =answer [1]value =2id =radio-1-2/>
< label for =radio-1-2> 23< / label>
< / li>
< / ul>
< / fieldset>
< / form>
我需要获取fieldset元素的索引。我目前正在使用这个每个迭代器(我不想改变它,因为它里面有很多其他的函数)。
$ b
});
我想让qIndex成为fieldset相对于表单的索引。例如,在这种情况下,我应该使它等于0和1(虽然会有6个值,因为它循环遍历列表元素)。
我玩过一段时间并且一直无法获得正确的索引。我一直没有找到(-1)。
如果有帮助,这里是我用来获取列表项索引相对于它包含ul元素的代码。
$ b
index = $('li',$(this).parent())。index (这个);
我的fieldset索引抓取代码有什么问题?
UL
,而不是 fieldset
。我认为这会为您设置
qIndex
: qIndex = $('fieldset')。index($(this).parents('fieldset'));
I am trying to get the index of an element using jQuery to send to a PHP script.
Here is my XHTML
<form action="/accreditation/questions/" method="post" id="questions-form">
<fieldset id="question-0">
<legend>Question</legend>
<h3>What colour is grass?</h3>
<ul>
<li>
<input type="radio" name="answer[0]" value="0" id="radio-0-0" />
<label for="radio-0-0">Green</label>
</li>
<li>
<input type="radio" name="answer[0]" value="1" id="radio-0-1" />
<label for="radio-0-1">Red</label>
</li>
<li>
<input type="radio" name="answer[0]" value="2" id="radio-0-2" />
<label for="radio-0-2">Orange</label>
</li>
</ul>
</fieldset>
<fieldset id="question-1">
<legend>Question</legend>
<h3>how old is alex</h3>
<ul>
<li>
<input type="radio" name="answer[1]" value="0" id="radio-1-0" />
<label for="radio-1-0">21</label>
</li>
<li>
<input type="radio" name="answer[1]" value="1" id="radio-1-1" />
<label for="radio-1-1">11</label>
</li>
<li>
<input type="radio" name="answer[1]" value="2" id="radio-1-2" />
<label for="radio-1-2">23</label>
</li>
</ul>
</fieldset>
</form>
I need to get the index of the fieldset elements. I am currently using this each iterator (which I'd like not to change because it has a lot of other functions inside it).
$('#questions-form ul li').each(function() {
qIndex = $('fieldset').index($('fieldset', $(this).parent()))
});
I'd like qIndex to be the index of fieldset relative to the form. For example, in this case I should have it equal to 0 and 1 (although there would be 6 values because it's looping through the list elements).
I have played around for a while and have been unable to get the correct index. I keep getting not found (-1).
If it helps, here is the code I'm using to get the list item's index relative to it's containing ul element.
index = $('li', $(this).parent()).index(this);
What is wrong with my fieldset index grabbing code?
The LI
's parent is UL
, not the fieldset
.
I think this will set qIndex
for you:
qIndex = $('fieldset').index($(this).parents('fieldset'));
这篇关于我如何使用jQuery获取此元素的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!