使用jquery拆分URL

时间:2017-03-24 17:37:46

标签: javascript jquery string url

我尝试使用jquery拆分url,但它给了我错误。

网址

<form action="http://localhost:8000/test/checkout/cart/add/uenc/aHR0cDovL2xvY2FsaG9zdDo4MDAwL3VtYS9wcC1wbGF0dGVuLWdyYXUtY29uZmlndXJlLXRlc3QuaHRtbA,,/product/1257/form_key/zcxhPfw8sSn5XgdI/" method="post" id="product_addtocart_form">

贝娄是我的代码

var formUrl = jQuery("#product_addtocart_form").attr("action");
var result = jQuery(formUrl).split('/product');
alert( result[1] );

错误

Error: Syntax error, unrecognized expression: http://localhost:8000/test/checkout/cart/add/uenc/aHR0cDovL2xvY2FsaG9zdDo4MDAwL3VtYS9wcC1wbGF0dGVuLWdyYXUtY29uZmlndXJlLXRlc3QuaHRtbA,,/product/1257/form_key/zcxhPfw8sSn5XgdI/

1 个答案:

答案 0 :(得分:2)

String#split不支持

jQuery。请改用纯粹的js。

var elem = document.getElementById('product_addtocart_form'),
    result = elem.getAttribute('action').split('/product');
    console.log(result[1]);
<form action="http://localhost:8000/uma/checkout/cart/add/uenc/aHR0cDovL2xvY2FsaG9zdDo4MDAwL3VtYS9wcC1wbGF0dGVuLWdyYXUtY29uZmlndXJlLXRlc3QuaHRtbA,,/product/1257/form_key/zcxhPfw8sSn5XgdI/" method="post" id="product_addtocart_form">