0

Is it possible to send selected options with javascript to the url ? In my case it seems to be the only solution. My code is very basic. My problem is also, that i have to handle smarty (i hate smarty)

{block name="frontend_detail_buy"}

<select name="lieferperiode" id="lieferperiode">
    <option value="{$sArticle.ordernumber|replace:"a":"a"}">jede Woche</option>
    <option value="{$sArticle.ordernumber|replace:"a":"b"}">alle 2 Woche</option>
    <option value="{$sArticle.ordernumber|replace:"a":"c"}">alle 3 Woche</option>
    <option value="{$sArticle.ordernumber|replace:"a":"d"}">jeden Monat</option>
</select>

<select name="menge" id="menge">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
</select>

<script type="text/javascript">
var lieferung = document.getElementById("lieferperiode");
var periode = lieferung.options[lieferung.selectedIndex].value;

var mengen = document.getElementById("menge");
var anzahl = mengen.options[mengen.selectedIndex].value;

var link = "http://XXXXX.de/XXXXX.php/sViewport,checkout/sAction,addArticle/sAdd," + periode + "/sQuantity," + anzahl;

{/block}

As you can see, the link contains 2 vars that are changeable via the dropdown menu. i made a test with alert and document.write and it is always giving me error var link is undefined. what am i doing wrong guys? cheers

4

2 回答 2

1

首先,您的 html 无效,因为您有:

<option value="{$sArticle.ordernumber|replace:"a":"a"}">jede Woche</option>

如果你要求这个 element.value 它会给你:{$sArticle.ordernumber|replace:

您可以将其更改为:

<option value='{$sArticle.ordernumber|replace:"a":"a"}'>jede Woche</option>

关于javascript,很有可能。而你正在这样做。您可能在将警报连接到您向我们展示的代码时遇到问题。

我能够使您的代码正常工作。看到这个jsfiddle

我刚刚添加了一个按钮来调用你的 javascript 并且它可以工作

于 2013-09-28T00:02:37.710 回答
0

正如 Filipe Silva 所提到的,首先,您必须将选项的代码更改为有效的 html 代码。

你的JS代码很好。问题可能是它在加载选择之前执行。所以我建议你使用jQuery库,它可以确保在执行代码之前完全加载 DOM。

我也建议您将代码包装在{literal}标记中,以便按原样显示而不被 Smarty 解释。

您的代码将是:

{literal}
<script type="text/javascript">
    $(document).ready(function(){

        var periode = $("#lieferperiode").find(":selected").val();

        var anzahl = $("#menge").find(":selected").val();

        var link = "http://XXXXX.de/XXXXX.php/sViewport,checkout/sAction,addArticle/sAdd," + periode + "/sQuantity," + anzahl;

        alert(link);
    });
</script>
{/literal}
于 2013-09-28T09:44:35.783 回答