dpmir1988 2013-10-14 12:08
浏览 50
已采纳

如何在单击FORM中的按钮时以HTML格式刷新多个文本区域(HTML内容最多4000个字符)?

I'm storing Data in PHP Two dimentional Array . Sample Values in PHP ARRAY is mentioned below:

First Element in Array
ID = 1
QUES= <p>ABCD</p>
OPT_A = <p>A</p>
OPT_B = <p>B</p>

Second Element in Array
ID = 2
QUES= <p> TWOABCD </p>
OPT_A = <p>TWA </p>
OPT_B = <p>TWB</p>

I am displaying values of QUEST, OPT_A, OPT_B in TEXTAREA in a HTML FORM (using TinyMCE jquery plugin for displaying rich text). The Form has the following buttons : NEXT & PREVIOUS.

On click of NEXT, I want to get TEXT AREAS refeshed with data from next element in array for all values. As per search over internet, I can achieve the same using AJAX, but not sure, how to refresh data of multiple text areas at the same time on click of a button in HTML FORM. Also note data is stored in the form of HTML Tags (with max length upto 4000 chars).

Currently, I'm using a very old method, of refreshing entire page on click of button on a form. It's having lot of drawbacks, including when the page gets refreshed, momentarily the user sees HTML tags before the page is loaded completely.

Can you please suggest any reference for (fixing these two issues) :

  1. Refreshing Data in Multiple Textares of a HTML-FORM on click of Button (inside HTML-FORM) using AJAX
  2. Avoiding momentary display of HTML code in Text area before page is completely loaded.

Updated CODE is mentioned below:

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
    <form>
        <input id="id" type="hidden" name="id">
        <p>Question:</p>
        <p id="myInstance3" style="border: 1px solid #000;"><textarea id="quest" readonly></textarea></p>
        <p>Option A:</p>
        <textarea id="opt_a" ></textarea>
        <p>Option B:</p>
        <textarea id="opt_b" ></textarea>
        <p>
            <button id="previous">Previous</button>
            <button id="next">Next</button>
        </p>
    </form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript"> 
var array = [{"id":1,"quest":"<p>ABC<\/p>","opt_a":"A","opt_b":"B"}, {"id":2,"quest":"<p>DEF<\/p>","opt_a":"1","opt_b":"2"}];
    var ArrayWalker = (function () {
        var _array = null,
            _index = 0,
            _walk  = function (step) {
                if (_array === null) {
                    throw 'Array is not initialized';
                }
                _index += step;
                if (_index < 0 || _index === _array.length) {
                    _index -= step;
                }
                return _array[_index];
            };
        return {
            init: function (array) {
                _array = array;
                _index = 0;
            },
            current: function () {
                return _walk(0);
            },
            next: function () {
                return _walk(1);
            },
            previous: function () {
                return _walk(-1);
            },
            index: function (seek) {
                if (seek !== undefined) {
                    _index = window.parseInt(seek);
                }
                return _index;
            }
        };
    })();

    function fillElements(data) {
        $('#id').val(data['id']);
        $('#quest').val(data['quest']);
        $('#opt_a').val(data['opt_a']);
        $('#opt_b').val(data['opt_b']);
    }

    ArrayWalker.init(array);
    fillElements(ArrayWalker.current());

    $('#previous').click(function (e) {
        e.preventDefault();
        fillElements(ArrayWalker.previous());
    });
    $('#next').click(function (e) {
        e.preventDefault();
        fillElements(ArrayWalker.next());
    });
</script>
<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">
     bkLib.onDomLoaded(function() {
          var myNicEditor = new nicEditor();
          myNicEditor.setPanel('myNicPanel');
          myNicEditor.addInstance('myInstance3');
     });
  </script> 
</body>
</html>
  • 写回答

1条回答 默认 最新

  • donglan7594 2013-10-16 15:36
    关注

    This method is based on traversing a given array that could be get via AJAX or directly on the page request.

    You can convert your PHP array into a Javascript one by doing this:

    // This is your given array
    $array = array(
        array(
            'id'    => 1,
            'quest' => '<p>ABC</p>',
            'opt_a' => 'A',
            'opt_b' => 'B',
        ),
        array(
            'id'    => 2,
            'quest' => '<p>DEF</p>',
            'opt_a' => '1',
            'opt_b' => '2',
        ),
    );
    
    // This is how you convert it to Javascript
    echo 'var array = ', json_encode($array), ';';
    

    Output:

    var array = [{"id":1,"quest":"<p>ABC<\/p>","opt_a":"A","opt_b":"B"}, {"id":2,"quest":"<p>DEF<\/p>","opt_a":"1","opt_b":"2"}];
    

    Now you need a Javascript object that can use that array in order to fill your textarea elements:

    var ArrayWalker = (function () {
        var _array = null,
            _index = 0,
            _walk  = function (step) {
                if (_array === null) {
                    throw 'Array is not initialized';
                }
                _index += step;
                if (_index < 0 || _index === _array.length) {
                    _index -= step;
                }
                return _array[_index];
            };
        return {
            init: function (array) {
                _array = array;
                _index = 0;
            },
            current: function () {
                return _walk(0);
            },
            next: function () {
                return _walk(1);
            },
            previous: function () {
                return _walk(-1);
            },
            index: function (seek) {
                if (seek !== undefined) {
                    _index = window.parseInt(seek);
                }
                return _index;
            }
        };
    })();
    

    Assuming this markup:

    <form>
        <input id="id" type="hidden" name="id" />
        <p>Question:</p>
        <textarea id="quest"></textarea>
        <p>Option A:</p>
        <textarea id="opt_a"></textarea>
        <p>Option B:</p>
        <textarea id="opt_b"></textarea>
        <p>
            <button id="previous">Previous</button>
            <button id="next">Next</button>
        </p>
    </form>
    

    This function will fill the elements:

    function fillElements(data) {
        $('#id').val(data['id']);
        $('#quest').val(data['quest']);
        $('#opt_a').val(data['opt_a']);
        $('#opt_b').val(data['opt_b']);
    }
    

    And finally you need to use your tools to get the job done:

    ArrayWalker.init(array);
    fillElements(ArrayWalker.current());
    
    $('#previous').click(function (e) {
        e.preventDefault();
        fillElements(ArrayWalker.previous());
    });
    $('#next').click(function (e) {
        e.preventDefault();
        fillElements(ArrayWalker.next());
    });
    

    Here is a working demo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面