【问题标题】:Change action link in a form when selecting an option with jquery使用 jquery 选择选项时更改表单中的操作链接
【发布时间】:2014-09-01 13:55:35
【问题描述】:

我想在选择选项时更改表单中的操作链接。选择31天选项时,下面的代码应更改操作链接。但我无法让它工作。而且我在控制台中看不到任何错误。我错过了什么吗?

我的 jquery 脚本:

<script type="text/JavaScript">
$("#zomlink").change(function() {
  var action = $(this).val() == "1906104";
  $("#buyvipform").attr("action", "http://www.somelink.com/?ref54434.swe");
});
</script>

我的表单代码:

<form method="post" id="buyvipform" name="buyvipform" class="buyvipform" action="https://www.changethislink.com">

<select name="zomlink" id="zomlink" style="margin-top: 290px;">
<option value="1906083">3 days Trial (2€)</option>
<option value="1906104">31 days (19€)</option>
<option value="1906125">90 days (50€)</option>
<option value="1906146">365 days (99€)</option>
</select>


<input type="submit" value="" style="background:url(<?php bloginfo('template_directory'); ?>/images/buy.png) no-repeat; width: 106px; height: 40px; border: 0px;">

</form>

【问题讨论】:

    标签: php jquery html wordpress


    【解决方案1】:

    您需要在选择更改时获取选定的选项值。然后如果它的值为1906104,则更改href url:

    $("#zomlink").change(function() {
     if($(this).val() == "1906104")
       $("#buyvipform").attr("action", "http://www.somelink.com/?ref54434.swe");
    });
    

    Working Demo

    【讨论】:

    • 是的,它在 jfiddle 中有效,但在我的网站上无效,当我观看控制台时,我没有收到任何错误。 Jquery 已加载,我正在从中运行其他东西。但由于某种原因,我无法加载此脚本。有没有其他方法可以在没有 Jquery 的情况下更改链接?例如可能使用 DOM?
    • 检查锚的ID并选择标签。还要确保事件已附加到 dom 就绪。
    • 这是一个我让它不起作用的小提琴,但它应该可以在 Javascript 中做这样的事情并跳过 jquery? jsfiddle.net/50sdfwen
    猜你喜欢
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2013-01-22
    • 2012-02-19
    • 1970-01-01
    相关资源
    最近更新 更多