扩展文本区域或在文本区域扩展时调用ajax都没有问题。但是,当我有三个文本区域时,每当我展开文本区域时,这三个文本区域都会调用ajax。我想调用任何扩展的文本区域。我不知道该怎么做。我尝试过$(this).closest('textarea').attr('id'),它不起作用。帮帮忙,谢谢。
$.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<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>
发布于 2015-04-15 04:10:15
问题是,您的3个textarea对象具有相同的类,并且当您使用类选择器时,您将ajax调用的结果放入所有的textarea中,并使用以下代码:
$('.expand9-999').html(data);您可以将文本区的id传递给load_expand_text函数,然后只为正确的文本区对象设置html。
您也可以尝试使用jQuery UI的resible插件,并将代码放入函数中,如下所示:
$("textarea").resizable({
resize: function() {
// your code and ajax call here, or something like this:
load_expand_text($(this))
}});和load_expand_text函数:
function load_expand_text(objtextarea){
... ajax call
success: function(data){
$(objtextarea).html(data);
}希望能有所帮助。
https://stackoverflow.com/questions/29634662
复制相似问题