我正在尝试使用jquery替换字符,我想将其传递给表单操作get方法。现在我获取文本字段的值并将其发送到get方法。我想检查并替换字符串中的一个字符然后传递它。我该怎么做?
我的代码:
<li>
<form action="Citizen/CitizenSearch" method="get">
<input class="cpr-search" type="text" name="cpr" />
<input class="cpr-search" name="submit" type="hidden" value="Søg" />
</form>
</li>
<script type="text/javascript">
$('#Søg').keypress(function (e) {
var cpr = $('#cpr').val();
if (cpr.indexOf("-") >= 0) {
cpr = cpr.replace("-", "");
}
if (e.which == 13) {
$(this).blur();
$('#submit').focus().click();
}
});
</script>
答案 0 :(得分:0)
我不确定如何使用name
属性访问文本字段,我会将它们用作ID。所以这是脚本。试一试。
<li>
<form action="Citizen/CitizenSearch" method="get">
<input class="cpr-search" type="text" id="cpr" />
<input class="cpr-search" name="submit" type="hidden" id="Søg" />
</form>
</li>
<script type="text/javascript">
$('#Søg').keypress(function (e) {
var cpr = $('#cpr').val();
cpr= cpr.replace(/[^A-Za-z0-9]/gi, '');
//This replace here will replace everything except alphanumeric values. gi is for global.
if (e.which == 13) {
$(location).attr('href', '/your_path?q=' + cpr);
//This statement will redirect you to your required location with the value of the text field that you intended.
//q is a get variable so that you can use it in next page.
}
});
</script>