首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何才能仅在使用jquery扩展的文本区域上调用ajax?

如何才能仅在使用jquery扩展的文本区域上调用ajax?
EN

Stack Overflow用户
提问于 2015-04-15 02:19:36
回答 1查看 62关注 0票数 2

扩展文本区域或在文本区域扩展时调用ajax都没有问题。但是,当我有三个文本区域时,每当我展开文本区域时,这三个文本区域都会调用ajax。我想调用任何扩展的文本区域。我不知道该怎么做。我尝试过$(this).closest('textarea').attr('id'),它不起作用。帮帮忙,谢谢。

代码语言:javascript
复制
$.fn.TextAreaExpander = function(minHeight, maxHeight) {

			var hCheck = !($.browser.msie || $.browser.opera);
		    var prevHeight;

			// resize a textarea
			function ResizeTextarea(e) {

				// event or initialize element?
				e = e.target || e;

				// find content length and box width
				var vlen = e.value.length, ewidth = e.offsetWidth;
				if (vlen != e.valLength || ewidth != e.boxWidth) {

					if (hCheck && (vlen < e.valLength || ewidth != e.boxWidth)) e.style.height = "0";
					var h = Math.max(e.expandMin, Math.min(e.scrollHeight, e.expandMax));

					e.style.overflow = (e.scrollHeight > h ? "auto" : "hidden");
					e.style.height = h + "px";
					
					if(e.style.height > prevHeight)  // throw the alert only if the height is not same as the previous one
						load_expand_text();// the function that will be called when expand
					e.valLength = vlen;
					e.boxWidth = ewidth;
			        prevHeight = e.style.height;  // save the height
					
				}

				return true;
			};

			// initialize
			this.each(function() {

				// is a textarea?
				if (this.nodeName.toLowerCase() != "textarea") return;

				// set height restrictions
				var p = this.className.match(/expand(\d+)\-*(\d+)*/i);
				this.expandMin = minHeight || (p ? parseInt('0'+p[1], 10) : 0);
				this.expandMax = maxHeight || (p ? parseInt('0'+p[2], 10) : 99999);

				// initial resize
				ResizeTextarea(this);

				// zero vertical padding and add events
				if (!this.Initialized) {
					this.Initialized = true;
					$(this).css("padding-top", 0).css("padding-bottom", 0);
					$(this).bind("keyup", ResizeTextarea).bind("focus", ResizeTextarea);
				}
				
			});

			return this;
		};



function load_expand_text(){
	var data={ 
            action: 'load_expand_text',
			current_load:'499',
		    userVoteNonce : UserAjaxVote.userVoteNonce,

};
		$.ajax({
			url: UserAjaxVote.ajaxurl,
			type:'POST',
			cache: false,
			data:data,
            beforeSend: function() {

  },
			success: function(data){
				$('.expand9-999').html(data);

			}
				});//ajax
				
			};//function load_expand_text
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<textarea class="expand9-999" id="exer_t"></textarea>
<textarea class="expand9-999" id="diet_t"></textarea>
<textarea class="expand9-999" id="run_t"></textarea>

EN

回答 1

Stack Overflow用户

发布于 2015-04-15 04:10:15

问题是,您的3个textarea对象具有相同的类,并且当您使用类选择器时,您将ajax调用的结果放入所有的textarea中,并使用以下代码:

代码语言:javascript
复制
$('.expand9-999').html(data);

您可以将文本区的id传递给load_expand_text函数,然后只为正确的文本区对象设置html。

您也可以尝试使用jQuery UI的resible插件,并将代码放入函数中,如下所示:

代码语言:javascript
复制
$("textarea").resizable({
resize: function() {
    // your code and ajax call here, or something like this:
   load_expand_text($(this))
}});

和load_expand_text函数:

代码语言:javascript
复制
function load_expand_text(objtextarea){
... ajax call
success: function(data){
        $(objtextarea).html(data);

        }

希望能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29634662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档