使用数据库条目自动填充 HTML 表单
我想通过单击链接自动填充两个输入元素。这是我正在使用的代码:
<script>
function autoFill(depart_from, depart_to) {
document.getElementById('depart_from').value = depart_from;
document.getElementById('depart_to').value = depart_to;
}
</script>
这是链接:
...
echo "<tr class='odd'>";
echo "<td><span class='hover'><ahref='#'onClick='autoFill(" . $row['departure'] . ", " . $row['destination'] . "return false;'>" . $row['departure'] ." » ". $row['destination'] . "</a><span></td>";
echo "</tr>";
无法让它工作!我做错了什么?
谢谢!
I want to autofill two input elements bi clicking on a link. Here is the code I'm using:
<script>
function autoFill(depart_from, depart_to) {
document.getElementById('depart_from').value = depart_from;
document.getElementById('depart_to').value = depart_to;
}
</script>
And here is the link:
...
echo "<tr class='odd'>";
echo "<td><span class='hover'><ahref='#'onClick='autoFill(" . $row['departure'] . ", " . $row['destination'] . "return false;'>" . $row['departure'] ." » ". $row['destination'] . "</a><span></td>";
echo "</tr>";
Can't get it to work! What am I doing wrong?
Thanx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于初学者来说,锚标记中的间距被打破:
应该是:
此外,您永远不会用分号关闭函数和分隔语句:
应该是:
最后,您可能需要将这些方法参数用引号括起来:
应该是:
可能还有更多。要记住的一件事是尽量不要使用
echo
语句来发出大块 HTML。它使代码不太容易阅读,更重要的是,它添加了另一层“字符串化”,这使得在字符串中包含引号变得稍微困难一些。For starters, the spacing is broken in your anchor tag:
should be:
Additionally, you never close the function and separate statements with a semi-colon:
should be:
Finally, you may want to wrap those method parameters in quotes:
should be:
There may be more. One thing to keep in mind is to try to not use
echo
statements to emit large blocks of HTML. It makes the code a little less easy to read, and more importantly it adds another layer of "stringification" which makes including quotes in strings slightly more difficult.a
标记上的间距错误,您需要在a href
之间留一个空格。您永远不会关闭函数调用的括号,我认为您可能需要将参数放在引号中,具体取决于它们的类型:
autoFill('" . $row['departure'] . "', '" $行['目的地']"');返回假;'
The spacing on your
a
tag is wrong, you need a space between thea href
.You never close the brackets for the function call, and I think you may need to put the parameters in quote marks, depending on their type:
autoFill('" . $row['departure'] . "', '" . $row['destination'] . "'); return false;'
我通过查看这个问题解决了这个问题: JQuery - Click Link Auto fill in输入框
I solved it by looking at this question: JQuery - Click Link Auto fill in input field