javascript - jQuery .insertAfter() 复制输入字段值

标签 javascript jquery html

我在这里创建了一个 html 网页:

http://diagrams.inse1d.info/wbt.html

代码:

<!DOCTYPE html>
<html>
<head>
    <title>WBT Charts</title>
    <link rel="stylesheet" type="text/css" href="wbt.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="wbt.js"></script>
    <link href='http://fonts.googleapis.com/css?family=Ubuntu:400,500,700' rel='stylesheet' type='text/css'>
</head>
<body>
    <section>
        <article>
            <div id="wbtBlock">
                <p>Press enter to save amendments</p>
                <h1>Title of wbt:<input type="text" id="wbtTitle" /></h1>
                <div id="graph">
                    <p>Graph to go here</p>
                </div>
                <p><strong>Notes: </strong></p>
                <p><span><input type="textarea" id="wbtNote" /></span></p>
            </div> 
        </article>
    </section>
    <button>Add New WBT Chart</button>
</body>

这是我编写的 jQuery 代码:

$(document).ready(function() {
$("h1").keypress(function(e) {
    //get
    wbtTitle = $('#wbtTitle').val();
    // Write text after enter
    if (e.which == 13) {
        $("h1").text(wbtTitle);
    }

});

$("span").keypress(function(e) {
    //get
    wbtNote = $('#wbtNote').val();
    // Write note after enter
    if (e.which == 13) {
        $("span").text(wbtNote);
    }

});

//Insert new WBT on button click
$("button").each(function() {
    $(this).click(function() {
        var wbtSet = $( "article" ).html();
        $(wbtSet).insertAfter('section');
    });
});
});

我想要做的是使用使用 jQuery 的输入框设置标题和一些注释文本。然后,我想在按下按钮时将 html 的副本添加到另一篇文章中,而不复制之前所做的输入,并可以在克隆文章时设置新值。这个过程应该一遍又一遍地重复。

这里有一张图片可以帮助解释: Explanation

我对 jQuery 很陌生,我认为你需要使用循环来解决这个问题,我读到可以使用 .each() http://api.jquery.com/jQuery.each/但不太确定。

任何帮助将不胜感激。谢谢。

最佳答案

我想我得到了你的答案。

我稍微改变了 html,因为你用你的方法重复了一些 id,这不好。 id 在页面上必须是唯一的。我只是将它们更改为类。

<!DOCTYPE html>
<html>
<head>
    <title>WBT Charts</title>
    <link rel="stylesheet" type="text/css" href="wbt.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="wbt.js"></script>
    <link href='http://fonts.googleapis.com/css?family=Ubuntu:400,500,700' rel='stylesheet' type='text/css'>
</head>
<body>
    <section>
        <article>
            <div class="wbtBlock">
                <p>Press enter to save amendments</p>
                <h1>Title of wbt:<input type="text" class="wbtTitle" /></h1>
                <div class="graph">
                    <p>Graph to go here</p>
                </div>
                <p><strong>Notes: </strong></p>
                <p><span><input type="textarea" class="wbtNote" /></span></p>
            </div> 
        </article>
    </section>
    <button>Add New WBT Chart</button>
</body>

当然还有 jQuery。我删除了你的前两个函数,因为它们在这种情况下没有任何用途。

$(document).ready(function() {
//Insert new WBT on button click
    $("button").on('click', function() {
        var title = $(this).prev().find('.wbtTitle').val();
        var note = $(this).prev().find('.wbtNote').val();
        var wbtSet = $(this).prev("section").html();       

        $(this).prev().find('.wbtTitle').replaceWith(title);
        $(this).prev().find('.wbtNote').replaceWith(note);

        $(this).prev("section").after('<section>' + wbtSet + '</section>');
    });
});

这是一个工作 fiddel

关于javascript - jQuery .insertAfter() 复制输入字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20109747/

相关文章:

javascript - 如何在正则表达式中使用变量 'object'?

javascript - 使用正则 exp 对货币进行四舍五入

javascript - 未捕获的类型错误 : Cannot read property 'network' of undefined in Chrome Extension

javascript - 在 JQuery 中使用复选框进行过滤时,如何在数据属性的完整字符串中进行搜索,而不仅仅是精确匹配?

javascript - 每次单击按钮时如何更改我的背景

javascript - 嵌入式 mp3 的 Quicktime 新外观

jquery - 背景位置淡化翻转效果

javascript - jquery 图像悬停在我悬停时不断增长

javascript - 使用 AJAX 传递图像文件不成功

asp.net - 为什么这个侧边栏背景颜色没有按照 CSS 中指定的那样更改?