简体   繁体   English

如何使用所见即所得的编辑器和jQuery提交文本区域

[英]How to submit a textarea using wysiwyg editor and jquery

Ok, because I am such a noob and have no idea how javascript works... My thinking was flawed. 好的,因为我真是个菜鸟,也不知道javascript的工作原理...我的想法有缺陷。 I am halfway there now. 我现在中途了。 I have taken one of the poster's suggestions and set up a function and then in the html form tag, I call it. 我已经接受了发布者的建议之一,并设置了一个函数,然后在html表单标签中将其命名。 This works. 这可行。 I thought that I needed to have an id selector to have JQuery trigger but that isn't the case. 我以为我需要一个ID选择器来具有JQuery触发器,但事实并非如此。 I'm sorry. 对不起。 I know I am not making any sense at all but please bear with me, I'm trying. 我知道我根本没有任何意义,但请您忍受,我正在尝试。 :) :)

Now that I have the form pointing to the javascript/jquery script. 现在,我有了指向javascript / jquery脚本的表单。 I now need to fix the jquery. 我现在需要修复jQuery。 Here is the code. 这是代码。

function doSave(){
  $(function() {
    $('.error').hide();
    $("#dfar").click(function() {
      $('.error').hide();

    var textArea = $('#dfarReport');
    if (textArea.val() == "") {
    alert('sd');
      textArea.show();
      textArea.focus();
    return false;
    }

For some reason, even though I press the submit button and have included the id selector of the text area I cannot see the alert. 由于某些原因,即使我按下了提交按钮并包含了文本区域的ID选择器,我仍然看不到警报。 Can someone please tell me what I am doing wrong? 有人可以告诉我我做错了吗?

ok, I think you'll need this: 好的,我想你需要这个:

$(document).ready(function(){

    $("form").submit(function(){
        var textArea = $('#dfarReport');

        if (textArea.val() == "") {
            textArea.show();
            textArea.focus();
            return false;
        }
        else // you'll need to add the else and the return true
        {
            return true; 
        }
    });

});

you need to return true when the form is valid. 您需要在表单有效时返回true。

More info: http://docs.jquery.com/Events/submit 更多信息: http : //docs.jquery.com/Events/submit

But make sure that you validate the data on the server as well! 但是,请确保还要验证服务器上的数据!

You could get the textarea by 您可以通过以下方式获得文本区域

$('textarea')

If it's the only one on your page. 如果这是您页面上的唯一页面。

You just need to call submit on the form that the textarea is contained within 您只需要以文本区域包含在其中的形式调用提交

EDIT: 编辑:

After reading your update, you just want to validate that the textarea is not blank before submitting the form. 阅读更新后,您只想在提交表单之前验证textarea是否为空白。 You just need to set up the validation 您只需要设置验证

var textArea = $('#results'); //Get the textarea

if (textArea.val() == "") {
    textArea.show();
    textArea.focus();
return false;
}
// perform the form submit here as textarea is not blank

Could you post the whole object where you have got the code beginning with 您可以将整个对象发布到代码开头的地方吗

 case "Save":

from? 从? It would probably be a good idea to use that object to perform the submit. 使用该对象执行提交可能是一个好主意。

EDIT 2: 编辑2:

In regard to your comment about the page being refreshed, submitting the form causes the page to be POSTed. 关于您对刷新页面的评论,提交表单会使页面过帐。 It sounds like you want to POST data using AJAX. 听起来您想使用AJAX发布数据。

Take a look at jQuery's $.ajax() command 看看jQuery的$.ajax()命令

Change button to 将按钮更改为

<script>
function doSave()
{
   // save data using jQuery
}
</script>
<input type="button" value="Save" onclick="doSave(); return false;">

or 要么

You can change the form with 您可以使用

<form onsubmit="doSave(); return false;">

You could do something like 你可以做类似的事情

$(document).ready(function(){
    $("#submit_btn").click(function(){
        mySubmit($("#myForm")) ; // call your existing javascript submit routine
    });
});

or if you prefer, 或者,如果您愿意,

$(document).ready(function(){
    $("#myForm").submit(function(){
        mySubmit($("#myForm")) ; // call your existing javascript submit routine
    });
});

please post the code you use. 请发布您使用的代码。

You edited like 3x and there's always a different approach. 您进行了3倍的编辑,总是有不同的方法。

Ok, if you want to use the code as you defined in your first post right now, you 'll have to do this: 好的,如果您现在想使用在第一篇文章中定义的代码,则必须这样做:

function doSave(){
  $(function() {
    $('.error').hide();
    $("#dfar").click(function() {
      $('.error').hide();

    var textArea = $('#dfarReport');
    if (textArea.val() == "") 
    {
      alert('sd');
      textArea.show();
      textArea.focus();
    return false;
    }
    else
    {
        return true;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM