[英]Use Jquery to append a field input value to the url as a query string
I believe I have most of this right, but may be messing up the order, and can't get a fiddle to save for some reason. 我相信我拥有大部分权利,但是可能会弄乱命令,并且由于某种原因无法为您节省资金。 Basically I need to grab part of a form Input and append it as a query string on submit so as to trigger the affiliate cookie on the next page.
基本上,我需要获取表单Input的一部分并将其作为查询字符串附加到Submit上,以便在下一页上触发联盟Cookie。 All codes would start with a AAA-11 value and then the actual affiliate reference code.
所有代码均以AAA-11值开头,然后是实际的会员参考代码。 So
所以
<form>
<label>Affiliate Code</label>
<input type="text" id="code">
<input type="submit" id="submit_btn">
</form>
And the JS: 和JS:
$('#submit_btn').on('click', function() {
var str = $('#code').val();
var acode = str.substr(6);
location.href = location.href + "?ref=" + acode;
});
What am I missing here? 我在这里想念什么?
Your button is submitting the form because of its attribute type="submit"
您的按钮正在提交表单,因为其属性为
type="submit"
You can change the attribute to type="button"
, or programmatically prevent form submission using event.preventDefault()
, so that you can modify the data. 您可以将属性更改为
type="button"
,或者使用event.preventDefault()
以编程方式阻止表单提交,以便您可以修改数据。
$('#submit_btn').on('click', function(event) {
event.preventDefault();
var str = $('#code').val();
var acode = str.substr(6);
location.href = location.href + "?ref=" + acode;
});
[EDIT] (comment) [编辑] (评论)
If you need to modify the form action
and submit the form aswell, use this: 如果您需要修改表单
action
并提交表单,请使用以下命令:
$('#submit_btn').on('click', function(event) {
event.preventDefault();
var str = $('#code').val();
var acode = str.substr(6);
// change form action attribute and submit the form:
$(this).closest('form').attr('action', location.href + "?ref=" + acode).submit();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.