简体   繁体   English

将文本框值以变量的形式附加到链接,而无需使用提交按钮

[英]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.

相关问题 不使用表单将文本框值分配给php变量 - Assigning textbox value to php variable without using form 提交表单而不使用提交按钮 - submit a form without using a submit button 如何在不使用提交按钮的情况下从html表单传递值 - How to pass value from html form without using submit button 使用链接提交表单并传递post变量 - Submit a form using a link and pass a post variable 获取没有提交按钮的已提交表单的价值 - Get value of a submitted form without submit button Goutte:如何提交没有价值的表单按钮? - Goutte: How to submit a form button without value? 如何通过单击按钮(不提交表单)将Textbox值获取到同一php文件中的php变量? - How to get Textbox value to a php variable within same php file with a button click (without submitting the form)? 从下拉列表中将选定的值分配给PHP变量onchange,而无需按html表单中的“提交”按钮? - Assigning a selected value from a dropdown list to a php variable onchange, without pressing submit button in a html form? 在没有提交按钮/表单的情况下将下拉值传递/获取到sql变量中 - Pass/get drop down value into an sql variable without submit button/form 如何在不使用提交按钮的情况下从文本框中加载输入? JavaScript的? - How to load input from textbox without using submit button? javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM