我有一个html页面,其中有一组选择菜单
,如下所示。 <label>Product Details</label>
<select id="PD">
<xsl:for-each select="PRODUCT_DETAILS">
<option value="{DATA0}"><xsl:value-of select="DATA0"></xsl:value-of></option>
</xsl:for-each>
</select>
<label>Price Details</label>
<select id="PRD">
<xsl:for-each select="PRICE_DETAILS">
<option value="{DATA1}"><xsl:value-of select="DATA1"></xsl:value-of></option>
</xsl:for-each>
</select>
如您所见,此选择菜单选项正在由一些多行 xml 填充。现在,如果我选择产品详细信息选项编号2,那么从价格详细信息中,选项编号2应该选择自动。
我只是想知道如何使用jquery获取选择的选项编号,例如如果我选择在产品详细信息中选择选项编号2,则结果应该给出2。
基于此值,我如何仅使用 jquery 使用选项编号在价格详细信息中选择选项编号 2。
我认为您正在寻找所选选项的索引
要得到
var index = $('#selectid option:selected').index(); // it gives 0 based index
要设置
$('#selectid option').eq(index).prop('selected', true)
演示:小提琴
尝试这样的事情
document.getElementById("mySelect").selectedIndex;
属性在下拉列表中设置或返回所选选项的索引。
指数从 0 开始。
$('#myselect').get(0).selectedIndex;
下面是一个简单的例子:
在 HTML 中:
<select id="myselect">
<option value="1">Mr</option>
<option value="2">Mrs</option>
<option value="3">Ms</option>
<option value="4">Dr</option>
<option value="5">Prof</option>
</select>
在 JS 中:
$( "#myselect option:selected" ).index();
在此处阅读有关 .index() 的信息。演示。