使用数据库条目自动填充 HTML 表单

发布于 2024-11-15 00:05:04 字数 657 浏览 5 评论 0原文

我想通过单击链接自动填充两个输入元素。这是我正在使用的代码:

 <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春庭雪 2024-11-22 00:05:04

对于初学者来说,锚标记中的间距被打破:

<ahref='#'onClick='autoFill(

应该是:

<a href='#' onClick='autoFill(

此外,您永远不会用分号关闭函数和分隔语句:

autoFill(" . $row['departure'] . ", " . $row['destination'] . "return false;

应该是:

autoFill(" . $row['departure'] . ", " . $row['destination'] . ");return false;

最后,您可能需要将这些方法参数用引号括起来:

autoFill(" . $row['departure'] . ", " . $row['destination'] . ");return false;

应该是:

autoFill('" . $row['departure'] . "', '" . $row['destination'] . "');return false;

可能还有更多。要记住的一件事是尽量不要使用 echo 语句来发出大块 HTML。它使代码不太容易阅读,更重要的是,它添加了另一层“字符串化”,这使得在字符串中包含引号变得稍微困难​​一些。

For starters, the spacing is broken in your anchor tag:

<ahref='#'onClick='autoFill(

should be:

<a href='#' onClick='autoFill(

Additionally, you never close the function and separate statements with a semi-colon:

autoFill(" . $row['departure'] . ", " . $row['destination'] . "return false;

should be:

autoFill(" . $row['departure'] . ", " . $row['destination'] . ");return false;

Finally, you may want to wrap those method parameters in quotes:

autoFill(" . $row['departure'] . ", " . $row['destination'] . ");return false;

should be:

autoFill('" . $row['departure'] . "', '" . $row['destination'] . "');return false;

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.

牵你手 2024-11-22 00:05:04

a 标记上的间距错误,您需要在 a href 之间留一个空格。

您永远不会关闭函数调用的括号,我认为您可能需要将参数放在引号中,具体取决于它们的类型:

autoFill('" . $row['departure'] . "', '" $行['目的地']"');返回假;'

The spacing on your a tag is wrong, you need a space between the a 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;'

江湖正好 2024-11-22 00:05:04

我通过查看这个问题解决了这个问题: JQuery - Click Link Auto fill in输入框

I solved it by looking at this question: JQuery - Click Link Auto fill in input field

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文