【问题标题】:Redirect a user on form submit based on a value in a radio button in the form根据表单中单选按钮中的值重定向用户提交表单
【发布时间】:2015-04-17 04:51:55
【问题描述】:

我需要制作一个使用 javascript 提交的简单表单,以将用户重定向到正确的页面。

它有一个单选按钮列表,提交时我需要将用户重定向到为每个单选按钮指定的页面 URL。

看起来像这样:

我找到了一个使用 jquery 来执行此操作的 javascript,但这是在单击单选按钮时触发的。我需要有一个提交按钮

我认为这很容易做到,而且可能是,但不知何故我找不到解决方案。

这是我使用的代码(我在 SO 上的其他地方找到了该代码,并且可以正常工作。但是没有“提交”按钮。):

I value the most:
<form>
  <input type="radio" name="redirect" value="http://example.com/food"><span>eating food</span><br>
  <input type="radio" name="redirect" value="http://example.com/kids"><span>play with my kids</span><br>
  <input type="radio" name="redirect" value="http://example.com/fishing"><span>go fishing</span><br>
  <input type="radio" name="redirect" value="http://example.com/stackoverflow"><span>answer StackOverflow questions</span><br>
</form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$('input[type="radio"]').on('click', function() {
     window.location = $(this).val();
});
</script>

我应该可以使用这个:https://api.jquery.com/submit/ 但我不明白如何同时触发提交和获取正确的单选值,然后重定向。

我可以以某种方式使用上面的代码,还是需要用完全不同的方法来实现它?

我试过这个:

I value the most:
<form id="mychoice">
  <input type="radio" name="redirect" value="http://example.com/food"><span>eating food</span><br>
  <input type="radio" name="redirect" value="http://example.com/kids"><span>play with my kids</span><br>
  <input type="radio" name="redirect" value="http://example.com/fishing"><span>go fishing</span><br>
  <input type="radio" name="redirect" value="http://example.com/stackoverflow"><span>answer StackOverflow questions</span><br>
  <input type="submit" value="Go">
</form>


<script>
$( "#mychoice" ).submit(function( event ) {
window.location = $('input[type="radio"]:checked').val();
});
</script>

这个想法是用我从选中的单选按钮中获得的值打开窗口位置。这可能是完全错误的方法。没用... :-)

【问题讨论】:

  • 你为什么使用表格?您想将数据发布到这些地址吗?如果没有,为什么不直接使用锚标签?
  • 是我的客户指定他们想要这样。他们希望它看起来和行为像带有单选按钮的常规表单。他们不希望在用户实际点击提交按钮之前提交。
  • 你只需要阻止默认的提交事件。在提交事件处理程序中尝试event.preventDefault()

标签: javascript jquery


【解决方案1】:

你的选择器有点不对劲。请尝试以下操作:

$('input[name="redirect"]:checked').val();

这个 JSFiddle 可能会有所帮助:

https://jsfiddle.net/yufnuwc6/2/

【讨论】:

  • 效果很好。只是为了添加答案(为了新读者)我添加了 'window.location = loc; ' 添加重定向。谢谢@贾斯汀奥伯
  • 您是否有理由建议使用名称进行选择而不是输入?
  • 嗯..在我的测试(本地)中,上述工作。但是当将它添加到实时域上的实时站点时,它没有。即使添加了“event.preventDefault();”,它仍然提交到“?redirect=http%3A%2F%2F”,而不是打开一个新站点。有什么想法吗?
  • 在提交函数底部放一个return false;
  • 谢谢。我测试了,但它没有工作。我进行了更多研究,看起来该特定页面上还有其他一些 javascript 可以让它像它一样处理。我试图编辑选择器和 var 名称,但它仍然表现得很奇怪。它是添加到网站主题中的 javascript。
【解决方案2】:

你可以试试这个

$( "#mychoice" ).submit(function( event ) {
    event.preventDefault();
    window.location = $('input[type="radio"]:checked').val();
});

【讨论】:

  • 不知何故奏效了。例如,它重定向到“example.com/index.php?redirect=http%3A%2F%2Fmydomain.com%2Ffood”,这意味着它尝试重定向,但没有重定向到新域。
  • 因为您没有对表单进行任何操作,所以您必须明确地处理提交操作,您已经这样做了。你需要做的只是告诉浏览器,你不必做你默认做的事情,所以 event.preventDefault()
  • 很高兴知道它有效。它会根据您的值重定向。
【解决方案3】:

使用按钮代替提交

<input type="button" id="submit" value="Go"> </form>

&lt;script&gt;

$( "#submit" ).click(function( event ) {
window.location = $('input[type="radio"]:checked').val();
});

&lt;/script&gt;

【讨论】:

    猜你喜欢
    • 2021-08-09
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 2018-10-12
    • 2013-04-07
    • 2023-03-29
    • 2013-01-21
    相关资源
    最近更新 更多