HTML输入没有更新值 [英] HTML input not updating value

查看:46
本文介绍了HTML输入没有更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常奇怪的问题(至少对我而言)。
我动态创建文本框,它工作正常。但问题是当我尝试写入那些,如果我检查html代码,我写的值不会出现。我不知道为什么会发生这种情况或如何解决这个问题。
这是我的代码示例。

I have a very strange problem (at least to me). I dynamically create textboxes, and it works fine. But the problem is when I try to write in those, if I check the html code, values that I write do not appear. I have no idea why this is happening or how to work arround this. Here is my code example.

<ul class="ml4 js-sortable-buttons list flex flex-column list-reset" data-disabled="false" id="page-content">
    <li class="p1 mb1 blue bg-white" id="id"><input type="text" class="textbox" /></li>
</ul>
<div class="center py2 ml4">
    <div class="js-add-textbox-button button blue bg-white" >add textbox</div>  
    <div class="show_html_button blue bg-white">show html</div>                     
</div>


<!-- <script src="../src/html.sortable.js"></script> -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script src="html.sortable.min.js"></script>
<script  type="text/javascript">
        sortable('.js-sortable-buttons', {
            forcePlaceholderSize: true,
            items: 'li',
            placeholderClass: 'border border-white mb1'

        });
        // buttons to add items and reload the list
        // separately to showcase issue without reload
        document.querySelector('.js-add-textbox-button').addEventListener('click', function () {
            doc = new DOMParser().parseFromString(`<li class="p1 mb1 blue bg-white"><input type="text" class="textbox"></li>`, "text/html").body.firstChild;
            document.querySelector('.js-sortable-buttons').appendChild(doc);
            sortable('.js-sortable-buttons');
            window.stop();
        });
</script>

    <script  type="text/javascript">
        $(".show_html_button").click(function () {
            var optionTexts = $("#page-content").clone(true);
            alert(optionTexts.html());
        });
</script>

我们非常感谢任何帮助。

Any help would be greatly appreciated.

推荐答案

这不是javascript的工作方式。当输入字段的值发生变化时,网页的HTML不会自动更新。

That's not how javascript works. A webpage's HTML doesn't automatically update when an input field's value changes.

如果你想要这样的功能,你可以这样做:

If you want such functionality, you could do this:

$(function(){
  $('.js-sortable-buttons').on('keyup change paste','input',function(){
    $(this).attr('value', $(this).val());
  });
});

这篇关于HTML输入没有更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆