私はjQueryのプラグインを持っている:jQuery Pluginオーサリング:プラグインを複数の値セットで操作するにはどうすればよいですか?
(function($){
$.fn.test = function(selector){
$this = this;
$(selector).bind("click", function(){
methods.showvalue($this);
});
};
var methods = {
showvalue : function($this){
var output = "";
$this.each(function(i,e){
output += $(e).val();
})
alert(output);
}
}
})(jQuery);
このマークアップ:
<form>
<p>
First section<br />
<input type="text" class="test" name="firstValue" value="one" />
<input type="text" class="test" name="secondValue" value="two" />
<input type="text" class="test" name="thirdValue" value="three" />
<button type="button" id="button1">push</button>
</p>
<p>
Second section<br />
<input type="text" class="test2" name="firstValue2" value="1" />
<input type="text" class="test2" name="secondValue2" value="2" />
<input type="text" class="test2" name="thirdValue2" value="3" />
<button type="button" id="button2">push</button>
</p>
</form>
と、この初期化子:
$(document).ready(function(){
// first section
$(".test").test("#button1");
// second section
$(".test2").test("#button2");
});
かかわらず、私は、私はいつも "との文字列を取得押したボタンのそれに123が入っています。私の目標は、2番目のボタンが押されたときに「123」を取得し、1番目のボタンが押されたときに「onetwothree」を取得することです。
私には何が欠けていますか?なぜそれはそのように振る舞いませんか?
return this.each(function() {
obj = $(this);
});
またはあなたの場合:
あなたがそうでなければvar $this = this;
を宣言する必要が
return this.each(function() {
$this = $(this);
$(selector).bind("click", function(){
methods.showvalue($this);
});
});
美しい警告! :-)ありがとう。 – quakkels