[英]Attaching textbox value to a link in the form of a variable without using submit button
I was wondering how it would be possible to attach an HTML form's text box value to a link. 我想知道如何将HTML表单的文本框值附加到链接。 For instance, I have 3 links and each link goes to a different PHP file, however, I need to pass a variable.
例如,我有3个链接,每个链接都指向不同的PHP文件,但是,我需要传递一个变量。 Because of the design of my page, I cannot use buttons.
由于页面的设计,我无法使用按钮。 So, I created a form with a text field.
因此,我创建了一个带有文本字段的表单。 How do I send that Text Fields value with the links?
如何通过链接发送“文本字段”值?
EG: 例如:
<form>
<input type="text" name="test1" id="test1"></input></form>
<a href=index.php?id=$test1">Link 1</a>
<a href=index2.php?id=$test1">Link 2</a>
<a href=index3.php?id=$test1">Link 3</a>
I hope this makes sense as to what I am trying to do. 我希望这对我要做什么有意义。
EDIT: 编辑:
I tried to do this dynamically, but: 我尝试动态执行此操作,但是:
OK, I made a function like this: 好的,我做了一个这样的函数:
<script>
function awardcode()
{ var awamount = document.getElementById("awardamount");
document.write('<?php $awardvar = ' + awamount.value + '; ?>');
};
</script>
Then, I made the link via PHP that looks like this: 然后,我通过PHP制作了如下所示的链接:
echo '<a id=awlink3 name=awlink3 href="index.php?siteid=gmaward&type=xp&post=' . $posts_row['posts_id'] . '&handle=' . $posts_row['handle'] . '&varamount=' . $awardvar . '">Award XP</a>';
However, that didn't work. 但是,那没有用。 This is my input box code:
这是我的输入框代码:
<form><input type=text name='awardamount' id='awardamount' onchange='awardcode()' style:'width:10px;'></form>
When I put the number and then tab, it loads an empty page. 当我输入数字然后按Tab时,它将加载一个空白页面。
How can I adjust that? 我该如何调整?
You'll need to dynamically change the link using JavaScript. 您需要使用JavaScript动态更改链接。 Here's one approach:
这是一种方法:
$('a').on('click',function(e) {
e.preventDefault();
var url = $(this).attr('href').split('$')[0];
window.location = url + $('#test1').val();
});
But it might be better to add an onchange
event to the textbox itself, so that the HREF changes instantly and the user can see the intended destination on mouseover: 但是最好在文本框中添加一个
onchange
事件,以便HREF立即更改,并且用户可以在鼠标悬停时看到预期的目标:
$('#test1').on('change', function () {
var val = $(this).val();
$('a[href*=\\?id\\=]').each(function (i, el) {
$(el).attr('href', function (j, str) {
return str.split('?')[0] + "?id=" + val;
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.