0

次のスクリプトがあります。それはjsFiddleで動作します 問題は、タイトルとテキストボックスを2回送信する必要がある場合です。

誰かが私が間違っていることを知っていますか?!

形:

<form id="formid" class="form" method="POST" action="/">
<div>
    <label class="title">Title</label>
    <div id="titleError"></div>
    <input type="text" id="title" name="title" value="">

</div>
<div>
    <label class="title">Text</label>
    <div id="textError"></div>
    <textarea name="text" id="text" rows="14" cols="50"></textarea><br />

</div>
<div>
    <input class="btn btn-success" type="submit" id="submitButton"  name="submitButton" value="Submit">
</div>

JS:

<script type='text/javascript'>
$(document).ready(function() {
  $("#formid").submit( function(event) {
        tinyMCE.triggerSave();
        var title = $('#title').val();
        var text = $('#text').val();
        if( title.length === 0 || text.length === 0 ){
            if( title.length === 0 ){
                $("#titleError").html("<p>Title verplicht</p>");
                event.preventDefault();
            }
            if( text.length === 0 ){
                $("#textError").html("<p>Text verplicht</p>");
                event.preventDefault();
            }

            $("html, body").animate({ scrollTop: 0 }, 600);
        }
        else{
            tinyMCE.triggerSave();

            /* stop form from submitting normally */
            event.preventDefault();

            /* Send the data using post */
            var posting = $.post( 'http://domain.nl/admin/pages/create', { 
                title: $('#title').val(), 
                text:  $('#text').val() 
            });

            /* Put the results in the show-content div */
            posting.done(function( data ) {
                //alert(data);
                $.ajax({
                    url: "<?php echo base_url() ?>/admin/pages",
                    type: 'GET',
                    success: function(data) {
                        $("#show-content").hide().html( data).fadeIn(1500);
                    }
                    ,
                    error: function() {
                        alert("error");
                    }

                });        
            });    

        }       
    });
});  

</script>

解決策:これを追加します

tinyMCE.triggerSave(); 

 $("#formid").submit( function(event) { 

正しく動作するようになりました。

4

0 に答える 0