【问题标题】:Jquery or JS code to change the GET URL parameters用于更改 GET URL 参数的 Jquery 或 JS 代码
【发布时间】:2012-12-10 06:04:17
【问题描述】:

我有一个带有复选框的页面,当它被选中时,它将通过 GET 提交值和参数,这是默认情况下的样子:

传递两个产品 SKU

http://example.com/product-comparisons/?product-skus=1&product-skus=2

只传递一个产品 SKU:

http://example.com/product-comparisons/?product-skus=1

或者这个:

http://example.com/product-comparisons/?product-skus=2

我想要完成的是仅在传递两个或多个产品时更改 GET 参数,例如:

http://example.com/product-comparisons/?product-skus=1&product-skus=2

进入这个:

http://example.com/product-comparisons/?product-skus=1,2

并且当没有选中的产品但提交了表单时,它会将 URL 更改为:

http://example.com/product-comparisons/

进入这个:

http://example.com/product-comparisons/?product-skus=0

即在 product-skus 查询字符串变量中添加 0。

我已经有一个 jQuery 代码,它会在点击提交按钮时运行:

jQuery( document ).ready( function( $ ) {
$('#myform').submit( function() {

    //change the GET URL parameters


 });

});

但我坚持完成剩下的过程。我希望在提交表单时发生这种情况。如果有人可以提供一些示例代码来入门,我会很高兴。非常感谢。

【问题讨论】:

    标签: jquery get


    【解决方案1】:

    我将提供一个不会直接回答您的问题的建议,但它可能会解决您的问题,并且可能会在此过程中为您节省大量工作和头痛。

    我假设您想要修改 GET 参数,因为您的服务器端应用程序仅在传递多个值时接收其中一个值,并且您希望拆分逗号分隔的参数来解决您的问题。

    有一个更简单的选择。

    将您的复选框name 属性更改为product_skus[],当您在服务器端应用程序上读取它时,它将作为数组检索。

    例如:

    http://example.com/product-comparisons/?product-skus[]=1&product-skus[]=2
    

    如果你是在 PHP 中阅读这篇文章:

    print_r($_GET['product_skus']);`
    

    会给你:

    Array(0 => '1', 1 => '2')
    

    希望对您有所帮助!

    (PS - 你也可以用其他服务器端语言来做到这一点,比如 Python,我相信大多数其他现代语言。Java 和 .NET 可以在没有 [] 语法的情况下做到这一点,所以我很漂亮确保你没有使用这些。)

    【讨论】:

      猜你喜欢
      • 2012-03-13
      • 2012-10-31
      • 2017-02-25
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      相关资源
      最近更新 更多