简体   繁体   English

我单击提交按钮时,用jQuery扩展了textarea,但未提交表单

[英]textarea expanded with jquery, not submitting the form when i click the submit button

I want the button to submit the form while the textarea is expanded. 我希望按钮在textarea扩展时提交表单。 How do i do this? 我该怎么做呢?

The problem is that when I click the Post button in the form, while the textarea is expanded, it does not submit the form, instead it shrinks back down, and I need to click it again while it is smaller, to submit the form. 问题是,当我单击表单中的“发布”按钮时,文本区域被扩展了,但它没有提交表单,而是缩小了,而当文本区域缩小时,我需要再次单击它以提交表单。

Currently in order to submit the post, i need to click the post button while the textarea is not expanded. 目前,为了提交帖子,我需要在文本区域未扩展的情况下单击发布按钮。

HTML HTML

<form method='POST' action='elsewhere.php'>
    <textarea id="textarea" style="width:550px; height:25px;"></textarea>
    <input class="btn" type='submit' value='Post'/>                             
</form>

jquery jQuery的

Currently when I click the text area, the height expands, and when i click elsewhere it shrinks. 当前,当我单击文本区域时,高度会扩大,而当我单击其他位置时,高度会缩小。 when i click the post button, while the textarea is expanded it doesnt submit the post, intead it shrinks the textarea back down. 当我单击“发布”按钮时,textarea展开时,它不提交帖子,而将其缩小。

$('#textarea').click(function(){
    $(this).css('height','100px')
    $(this).html('');
    $(this).blur(function(){
        $(this).css('height','25px')

    });
})

If you are submitting the form and redirecting to another page, all you need is for the button to stay still long enogh to register the click, a timeout solves that problem: 如果您要提交表单并重定向到另一个页面,则所需要做的只是使按钮停留很长时间来注册点击,超时可以解决该问题:

$('#textarea').on({
    focus: function() {
        $(this).css('height','100px').html('');
    },
    blur: function() {
        var self = this,
            T = setTimeout(function() {$(self).css('height','25px') }, 200);
    }
});

FIDDLE 小提琴

If using this with Ajax, and not redirecting, and to make the textarea not respond to the click on the button, you could use a variable to keep track of what element was clicked, and do somewhat the same as above: 如果将它与Ajax一起使用而不进行重定向,并且不使textarea对按钮的单击做出响应,则可以使用变量来跟踪单击了哪个元素,并执行与上面相同的操作:

var elm;

$('.btn').on('click', function(e) {
    elm = e.target;
    e.preventDefault();
    //do ajax stuff
});

$('#textarea').on({
    focus: function() {
        $(this).css('height','100px').html('');
    },
    blur: function() {
        var self = this,
            T = setTimeout(function() {
                if ($(elm).attr('class') != 'btn') $(self).css('height','25px');
            }, 200);
    }
});

FIDDLE 小提琴

I think its some z-index related issue. 我认为这是与z-index相关的问题。 Try giving position: relative; z-index:1; 尝试给出 position: relative; z-index:1; position: relative; z-index:1; to the submit button. 提交按钮。

If you have set z-index for the textarea or any of its ancestors, increment that value by one and set it to the button. 如果已为 textarea或其任何祖先设置了 z-index ,则将该值加1并将其设置为按钮。

There is a chance of the button being overlapped by the textarea while increasing its height. 在增加按钮高度的同时,按钮可能会与文本区域重叠。 And you too mentioned that, while the textarea is expanded it doesnt submit the post . 您也提到过, while the textarea is expanded it doesnt submit the post It may be because of the button is overlapped. 可能是因为按钮重叠。

Update: 更新:

I got it after 我知道了

The problem is that when I click the Post button in the form, while the textarea is expanded, it does not submit the form, instead it shrinks back down, and I need to click it again while it is smaller, to submit the form. 问题是,当我单击表单中的“发布”按钮时,文本区域被扩展了,但它没有提交表单,而是缩小了,而当文本区域缩小时,我需要再次单击它以提交表单。

$('#textarea').click(function(){
$(this).css('height','100px')
$(this).html('');
$(this).blur(function(){
    if(this.value === ''){
        $(this).css('height','25px')
    }

});
});

I had a similar issue the other day, and what was happening was that the blur event on the textarea was occurring before the browser took into consideration the click event on the button. 前几天我遇到了类似的问题,发生的事情是在浏览器考虑按钮上的click事件之前 ,发生了textarea上的blur事件。

Thus, in order, I was clicking on the button, THEN the browser fired the blur event on the textarea, shrinking it and moving the button, THEN the browser took the click in consideration, doing nothing because there was no longer a button where I clicked. 因此,按顺序,我单击了按钮,然后浏览器在文本区域上触发了blur事件,将其收缩并移动了按钮,然后浏览器考虑了单击,没有采取任何措施,因为在我没有按钮的地方点击。

I did not directly fix the problem, though, deciding that I would shrink the textarea only if it was not empty (adding some this.value == this.defaultValue || this.value == '' test in the blur event). 但是,我没有直接解决问题,而是决定仅在文本区域不为空时才缩小文本区域(在blur事件中添加一些this.value == this.defaultValue || this.value == ''测试)。

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

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