jqueryを使用してラジオに応じてテキスト入力のタイトルを変更するにはどうすればよいですか?
-
22-08-2019 - |
質問
ご覧のとおり http://jsbin.com/erivi/ Jqueryを使用して、選択したラジオボタンに応じていくつかの画像(および画像属性)を変更しています。また、その中をクリックするとフォームテキストを削除しています。
そして今私がやろうとしているのは、チェックされたラジオボタンに応じてフォームのテキストを変更することです。
たとえば、「選択肢 3」をクリックすると、「入力 3 のテキスト」が表示されます。
ここで私のコードをライブで表示および編集できます。 http://jsbin.com/erivi/edit
改善する必要がある部分は次のとおりです。
imgFldr = 'http://download.kde.org/khotnewstuff/icons/previews/';
$("input[type='radio']").click(function() {
var strarr = this.title.split('|');
if(strarr.length < 2) return;
$('#'+this.name).attr('src', imgFldr+strarr[0]).attr('alt', strarr[1]).attr('title', starr[2]);
});
そして特に .attr('title', starr[2]);
これは、ラジオのタイトルの 3 番目の部分に応じてタイトルが変更されることになっています (例: title='m602-1.png|Logo 3|Text of input 3'
)
ありがとう :)
編集:実際のコードでいくつかのフォームを使用していることを言い忘れていました。そのため、他のテキスト入力のスクリプトの繰り返しを避けるために、テキスト入力の特定の名前を使用せずにこれを行う方法を探しています。
解決
編集: 入力テキストボックスの ID は一意である必要があります。さらに、設定する必要があります .attr('title',strarr[2])
, .val(strarr[2])
ラジオ ボタンのクリック イベント内のヒント クラス。また、いくつかの変更を加える必要があります inputdynvalue
プラグイン。
更新された完全な作業コードは次の URL でご覧ください。 http://jsbin.com/akawo
プラグインコードを更新しました
(function($) {
$.fn.inputdynvalue = function (options) {
var opts = $.extend({}, $.fn.inputdynvalue.defaults, options);
return this.each(function(){
var hvalue = opts.htext;
switch (opts.htext) {
case 'title' : hvalue = $(this).attr('title'); break;
case 'value' : hvalue = $(this).attr('value'); break;
}
$(this).attr('value', hvalue).addClass(opts.hclass)
.unbind('focus blur')
.bind('focus', function() {
if (this.value === this.title) {
this.value = '';
$(this).removeClass(opts.hclass);
}
})
.bind('blur', function() {
if (this.value === '') {
this.value = this.title;
$(this).addClass(opts.hclass);
}
});
});
};
$.fn.inputdynvalue.defaults = {
htext: 'title',
hclass: 'hint'
};
})(jQuery);
$(document).ready(function(){
$("input[type='text']").inputdynvalue();
});
更新されたラジオ ボタンのクリック イベント ハンドラー
$("input[type='radio']").click(function() {
var strarr = this.title.split('|');
if(strarr.length < 2) return;
$('#'+this.name).attr('src', imgFldr+strarr[0]).attr('alt', strarr[1]);
$('#'+this.name+'_text')
.attr('title',strarr[2])
.val(strarr[2])
.addClass('hint');
});
他のヒント
これを試してください:
<form method="post" action="">
<div>
<img id="iymok" src="http://download.kde.org/khotnewstuff/icons/previews/m732-1.png" alt="Alt by default" />
<input type="text" id="iymok_text" title="Blablabla Text 1" />
<label><input type="radio" checked="checked" name="iymok" title="m133-1.png|Logo 1|Blablabla Text 1" />Choice 1</label>
<label><input type="radio" name="iymok" title='m203-1.png|Logo 2|Text of input 2' />Choice 2</label>
<label><input type="radio" name="iymok" title='m602-1.png|Logo 3|Text of input 3' />Choice 3</label>
</div>
</form>
<script type="text/javascript">
imgFldr = 'http://download.kde.org/khotnewstuff/icons/previews/';
$("input[type='radio']").click(function() {
var strarr = this.title.split('|');
if (strarr.length >= 3) {
$('#'+this.name).attr('src', imgFldr+strarr[0]).attr('alt', strarr[1]).attr('title', strarr[2]);
$('#'+this.name+'_text').attr('title', strarr[2]);
}
});
</script>