jQuery 如何在模态对话框中设置输入值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7845866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:20:48  来源:igfitidea点击:

How to set the input value in a modal dialogue?

jquerytwitter-bootstrapmodal-dialogjquery-events

提问by Arman

I'm working on 'add a link' functionality. For that I'm using Modal plugin from Twitter Boostrap JS. On the main page there's only the 'link' field to fill, when a user clicks 'add link' button, a modal pops up, and the user sees the complete form to fill 3 fields: link, title, tags. However, I want the link field to be pre-filled with the value from the previous step. It's similar to what happens when you 'save a link' on delicious.com.

我正在研究“添加链接”功能。为此,我使用来自 Twitter Boostrap JS 的 Modal 插件。在主页上只有“链接”字段要填写,当用户单击“添加链接”按钮时,会弹出一个模式,用户会看到完整的表单来填写 3 个字段:链接、标题、标签。但是,我希望使用上一步中的值预先填充链接字段。这与您在美味网站上“保存链接”时发生的情况类似。

Thanks to Joe's help (previous question) I can print the linkURL on the modal dialogue, but it's important that this linkURL is the value of the link field of the form with 3 fields (link, title, tags). Any idea how this can be done?

感谢 Joe 的帮助(上一个问题),我可以在模态对话框上打印 linkURL,但重要的是,此 linkURL 是具有 3 个字段(链接、标题、标签)的表单的链接字段的值。知道如何做到这一点吗?

<html>
<head>
  <title>Example</title>
  <script src="scripts/jquery.min.js"></script>
  <script src="scripts/bootstrap-modal.js"></script>
  <link rel="stylesheet" href="scripts/bootstrap.min.css">
  <link rel="stylesheet" href="main.css" />

  <script type="text/javascript">
            $(document).ready(function()
            {
                $('#modal-from-dom').bind('show',function()
                {
                    $(".modal-body").html($("#linkURL").val());
                });
            });
    </script>
</head>
<body>
  <!-- The Modal Dialog  -->
          <div id="modal-from-dom" class="modal hide fade">
            <div class="modal-header">
                <a href="#" class="close">&times;</a>
                <h3>Add Link</h3>
            </div>
            <div class="modal-body">
            <!--
            <form id='post-on-wall' method='POST' action='savePost.php' enctype='multipart/form-data'>"
        <input type='text' class='label-inline' name='linkURL' id='wall-post' value=linkURL>
            </form>
            -->
            </div>
            <div class="modal-footer">
                <a href="#" class="btn primary">Add Link</a>
            </div>
        </div>
  <div class="container">
    <div class="wall-post">
        <textarea class='label-inline' name='linkURL' id='linkURL'></textarea>
        <button data-controls-modal="modal-from-dom" data-backdrop="true" data-keyboard="true" class="btn">Add Link</button>
    </div>
  </div>

</body>

回答by Marco Johannesen

      $('#modal-from-dom').bind('show',function()
            {
                $(".modal-body #wall-post").val($("#linkURL").val());
            });

or am i missing something? :)

还是我错过了什么?:)