从其他字段中的“选择”获取数据

时间:2013-07-13 20:02:34

标签: javascript select return-value

有人可以帮助我,我已经搜索过但没有找到我想要的东西,取决于在其他领域做出的选择取得所选选项的值,例如Value&名称。

更新: 谢谢我尝试添加“id”,如果它工作,但链接名称“不”。修改脚本如下,以便在我的网站上的两个结果中进行更多定义。可以在此处查看:jsfiddle.net/87d5C/2

你能帮助名字价值吗?

HTML:

<select id="Product1">
    <option name="billingcycle=monthly" value="16.00">1 Month</option>
    <option name="billingcycle=semiannually" value="18.00">6 Months</option>
    <option name="billingcycle=annually" value="20.00">12 Months</option>
</select>
<ul>
    <li id="price1">16.00</li>
</ul>
<ul>
    <li id="price1"><a href="cart.php?a=add&pid=300&">Order</a></li>
</ul> 

脚本:

var select = document.getElementById("Product1"),
    li = document.getElementById("price1"),
    link = li.querySelector("[href='cart.php?a=add&pid=300&']"),
    option;
select.addEventListener("change", function(e){
    option = select.options[select.selectedIndex];
    li.innerHTML = option.value;
    link.setAttribute("href", "cart.php?a=add&pid=300&"+option.name);
});

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

尝试此操作(您可能需要在selectul元素中添加ID,以确保您获得正确的ID:

var select = document.getElementById("Product1"),
    price = document.getElementById("price1"),
    link = document.getElementById("order1").firstChild,
    option;
select.addEventListener("change", function(e){
    option = select.options[select.selectedIndex];
    price.innerHTML = option.value;
    link.setAttribute("href", "cart.php?a=add&pid=300&"+option.getAttribute("name"));
});

请告诉我这是否适合您。

相关问题