如何使用javascript将多个变量附加到网址?

时间:2012-03-05 07:57:04

标签: javascript html url

我无法使用javascript将多个变量附加到网址的末尾。用户使用选择菜单和文本区域在页面上提供输入。仅使用textarea可以工作,但我无法包含选择菜单。

<?php
$price = $_GET['price'];
$title = $_GET['title'];
$retailer = $_GET['retailer'];
$img = $_GET['img'];
$link = $_GET['link'];
$desc = $_GET['desc'];    
?>

<textarea rows="3" id="textarea" class="input-xlarge" placeholder="Size, color, style, etc" name ="options"></textarea>

<select style="width: 130px;" id="quantity" class="span2">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        </select>

<script language="javascript">
    function test()
{
    var val=document.getElementById("textarea").value;  
    var val=document.getElementById("quantity").value; 
    var hrf="viewcart.php?retailer=<?php echo $retailer?>&link=<?php echo $link; ?>&price=<?php echo $price; ?>&title=<?php echo $title; ?>&options="+val;
    document.getElementById("a_link").href=hrf;
}
</script>

<a href ="#" id="a_link" onclick="test();" class="btn btn-success" type="submit">
    <i class="icon-shopping-cart icon-white"></i>  Add to Cart
</a>

我想改变的是:

    var hrf="viewcart.php?retailer=<?php echo $retailer?>&link=<?php echo $link; ?>&price=<?php echo $price; ?>&title=<?php echo $title; ?>&options="+val;

我尝试了以下但是没有用:

    var hrf="viewcart.php?retailer=<?php echo $retailer?>&link=<?php echo $link; ?>&price=<?php echo $price; ?>&title=<?php echo $title; ?>&options="+val + “&quantity=” + quantity;

有关如何使其发挥作用的任何想法?

2 个答案:

答案 0 :(得分:1)

使用此好友,

var quantity = document.getElementById("quantity").options[document.getElementById("quantity").selectedIndex].value;

var val=document.getElementById("textarea").value;

var hrf="viewcart.php?retailer=<?php echo $retailer?>&link=<?php echo $link; ?>&price=<?php echo $price; ?>&title=<?php echo $title; ?>&options="+val + “&quantity=” + quantity;

答案 1 :(得分:-1)

您无法使用value访问下拉列表的选定选项。您必须找到选择了哪个选项,然后找到该选项的值。试试这个:

var selected_qty = document.getElementById("quantity").selectedIndex;
var all_qtys     = document.getElementById("quantity").options;

var quantity = all_qtys[selected_qty].value;