按 <a> 属性时更改输入值

Change input value when i press <a> Attribute

好的,伙计们已经在搜索它了,但根本没有结果,也不知道该怎么做,所以我的问题是:

我有这个表格:

<form method="POST" action="search.php" name="form" id="form">
<input type="text" name="search" id="search" placeholder="Introduza a sua Localização">
<input type="submit" name="Submit" value="Pesquisar">
</form>

我想要的是当我按下这个按钮时:

<a onClick="">City</a>

在这种情况下输入值更改为我想要的"City"然后自动提交表单。

我已经对此进行了研究,但我无能为力,我们将不胜感激。

凹陷。

伙计们,感谢您的帮助,现在可以正常工作了,但是有人请告诉我这段代码中的问题是什么:

<?php if (mysql_num_rows($tournaments) !=0){
     do { ?>
     <div id="mainContainer">
     <div id="leftContainer"><img src="images/tournaments/<?php echo $row_tournaments['logo']; ?>.png"></div>
     <div id="rightContainer">
     <div id="rightContent">
     <p><?php echo $row_tournaments['description']; ?></p>
     <a href="tournaments.php?id_tournament=<?php echo $row_tournaments['id_tournament']; ?>"><div id="galleryButton"><p>Entrar no torneio</p></div></a>
     </div>
     <div id="rightDetails">
     <i class="fa fa-gamepad"></i> <a href="games.php?id_game=<?php echo $row_tournaments['id_game']; ?>"><?php echo $row_tournaments['game']; ?></a><br>
<i class="fa fa-calendar-o"></i> <a href="#"><?php echo $row_tournaments['date']; ?></a><br>
<i class="fa fa-pencil-square-o"></i> <a href="tournaments.php?id_tournament=<?php echo $row_tournaments['id_tournament']; ?>#disqus_thread">Sem comentários</a><br>

<script type="text/javascript">
function giveThatInputAValue(){
    var elem = document.getElementById("search");
    elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();
*/}
</script>
<i class="fa fa-map-marker"></i> <a onClick="giveThatInputAValue()"><?php echo $row_tournaments['city']; ?></a><br>


<img src="images/<?php echo $row_tournaments['online']; ?>.png"> <a href="#"><?php echo $row_tournaments['online']; ?></a>
</div>
</div>
</div>
<?php } while ($row_tournaments = mysql_fetch_assoc($tournaments));
} else {
?>
<div id="noresults"><p>Sem torneios</p></div>
<?php
}
?>

一切都很好,但那部分:

<script type="text/javascript">
function giveThatInputAValue(){
var elem = document.getElementById("search");
elem.value = "<?php echo $row_tournaments['city']; ?>";
/*document.forms["form"].submit();
*/}
</script>

它不是循环它只显示第一条记录这是为什么?

如有任何帮助,我们将不胜感激。

在您的 js 文件中,您可以检查文本框的值是否等于您想要的城市名称。如果是,则提交表格。将函数附加到您的 a tag

function SubmitRightCity(){
   if(document.getElementById('search').value == "citynamehere"){
        document.forms["form"].submit();
   }
}

在html

<a onClick="submitRightCity()">City</a>

在您希望 a taginput 赋值的情况下,您将一个函数附加到您的标签,一旦 link 被点击,该函数将给你的输入一个值。

<a onClick="giveThatInputAValue()"> City</a>

在 javascript 文件中,您将拥有

<script>
    function giveThatInputAValue(){
         var elem = document.getElementById("search");
         elem.value = "My city value";

         //if you want to submit the form right after, you add the line below
         document.forms["form"].submit();
    }
</script>