如何通过jQuery通过replace方法更新textarea [英] How to update textarea with replace method through jQuery

查看:89
本文介绍了如何通过jQuery通过replace方法更新textarea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本区域,其中将包含用户输入的代码,我想获取该代码并使用jQuery对其进行扫描,以获取名为setting的自定义标记中的值,然后将该值添加至输入中,以便用户能够在不触摸代码的情况下更改设置标签内的值.我能够获取值并将其添加到输入中,但是无法使用新值更新代码.

I have a textarea that will contains a code entered by user and I want to get that code and scan it with jQuery to get the value inside a custom tag called setting then add this value to an input so the user will be able to change the value inside setting tag without touching the code. I was able to get the values and add them inside the inputs but I couldn't update the code with the new values.

HTML代码:

<div id='tab-1'>
  <textarea id='template-code' cols='67' rows='27'></textarea>
  <button id='submit-code'>Submit Code</button>
</div>

<div id='tab-2' class='unactive'>
  <form id='settings-form' method='POST'>
    <div id='result'></div>
    <button id='update-code'>Update Code</button>
  </form>
</div>

CSS代码:

.unactive {
    display: none
}

jQuery代码:

$('#template-code').change(function (){

  var $that = $(this),
      template_code = $that.val(),
      code = '',
      new_data = '',
      text = '',
      newCode = '';

  // Extract settings from the theme and add them to #result              
  $(document).on('click', '#submit-code', function (){

      $('#tab-1').addClass('unactive');
      $('#tab-2').removeClass('unactive');

      $(template_code).find('setting').each(function (i){

        var $this = $(this),
            setting_std = $this.text(),
            setting_id = $this.attr('id');

        code += '<input id="'+setting_id+'" name="'+setting_id+'" type="text" value="'+setting_std+'"><br>';

      });

      if(code !== ''){
        $('#result').html(code);
      }

  });

  // Update old data with the new one
  $(document).on('click', '#update-code', function (){

    new_data = $( "#settings-form" ).serializeArray();
    $.each( new_data, function( i, new_field ) {

        var start_key = "id='"+new_field.name+"'>",
            end_key = '</setting>',
            start  = template_code.indexOf(start_key), 
            end = template_code.indexOf(end_key);

        text = template_code.substring(start + start_key.length, end);

        // THE PROBLEM IS HERE
        // I want the variable template_code to contains the new value not the old one so I used replace but it seems that it doesn't work
        template_code.replace(text, new_field.value);


    });

    $('#template-code').val(template_code);
    $('#tab-1').removeClass('unactive');

    return false;

  });

});

这是将添加到textarea内的主题代码的示例:

This is an example of the theme code that will be added inside the textarea :

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html b:version='2' class='v2' expr:dir='data:blog.languageDirection' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'>
  <head>

    <b:include data='blog' name='all-head-content'/>

    <link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,300italic,700' rel='stylesheet' type='text/css'/>
    <link href='http://fonts.googleapis.com/css?family=Lora:400,400italic,700,700italic' rel='stylesheet' type='text/css'/>
    <link href='https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css' rel='stylesheet'/>

    <title><data:blog.pageTitle/></title>

    <div id='option-panel' style='display:none!important'>

      <setting id='post_thumbnail'>http://lorempixel.com/640/300/</setting>
      <setting id='search_icon'>on</setting>

    </div>
</head>
<body>

</body>
</html>

要了解我的问题,请输入此 JsFiddle 并复制上面的代码,然后将其放入textarea并单击提交代码,您将获得两个输入,这些输入的内容来自这两个标记:

To understand my issue please enter to this JsFiddle and copy the code above then put it inside the textarea and click submit code, you will get two inputs the content of those inputs come from these two tags :

<setting id='post_thumbnail'>http://lorempixel.com/640/300/</setting>
<setting id='search_icon'>on</setting>

我希望当用户更改输入的值并单击更新代码"以更改整个代码中的设置标签的值.

I want when the user change the value of inputs and click "update code" to change the value of setting tag inside the entire code.

推荐答案

这是该代码的另一个版本.我将新值保存在数组中,然后将它们替换为textarea文本中的现有值.试试看,看是否能解决您的问题.

Here's another version of the code. I saved the new values in an array and then replaced them with the existing values in the textarea text. Give a try and see if that solves your problem.

脚本:

 <script type="text/javascript">
    $('#template-code').change(function () {

        var $that = $(this),
            template_code = $that.val(),
            code = '',
            new_data = '',
            text = '',
            newCode = '';

        // Extract settings from the theme and add them to #result              
        $('#submit-code').click(function () {

            $('#tab-1').addClass('unactive');
            $('#tab-2').removeClass('unactive');

            $(template_code).find('setting').each(function (i) {

                var $this = $(this),
                    setting_std = $this.text(),
                    setting_id = $this.attr('id');

                code += '<input id="' + setting_id + '" name="' + setting_id + '" type="text" value="' + setting_std + '"><br>';

            });

            if (code !== '') {
                $('#result').html(code);
            }

        });

        // Update old data with the new one
        $('#update-code').click(function () {

            new_data = $("#settings-form").serializeArray();

            $(template_code).find('setting').each(function (i) {
                template_code = template_code.replace("<setting", "").replace("id='" + $(this).attr("id") + "'>", "").replace($(this).html(), "{" + i + "}").replace("</setting>", "");
            });

            $.each(new_data, function (i, new_field) {
                    template_code = template_code.replace("{" + i + "}", "<setting id='" + new_field.name + "'>" + new_field.value + "</setting>");
            });

            $('#template-code').val(template_code);
            $('#tab-1').removeClass('unactive');

            return false;

        });

    });
</script>

HTML模板:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html b:version='2' class='v2' expr:dir='data:blog.languageDirection' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'>
  <head>

    <b:include data='blog' name='all-head-content'/>

    <link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,300italic,700' rel='stylesheet' type='text/css'/>
    <link href='http://fonts.googleapis.com/css?family=Lora:400,400italic,700,700italic' rel='stylesheet' type='text/css'/>
    <link href='https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css' rel='stylesheet'/>

    <title><data:blog.pageTitle/></title>

    <div id='option-panel' style='display:none!important'>

      <setting id='post_thumbnail'>text1</setting>
      <setting id='search_icon'>text2</setting>

    </div>
</head>
<body>

</body>
</html>

我无法替换您提供的模板中的文本"on",不确定是否与某个保留的关键字有关,但是其他所有内容都可以正常工作.

I couldn't replace the text 'on' in the template you provided, not sure if it has something to do with some reserved key word but everything else works fine.

这篇关于如何通过jQuery通过replace方法更新textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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