1

http://jsbin.com/erivi/でわかるように、Jquery を使用して、選択したラジオ ボタンに応じて一部の画像 (および画像属性) を変更しています。また、その中をクリックすると、フォーム テキストも削除されます。

そして今、私がやろうとしているのは、チェックされたラジオボタンに応じてフォームのテキストを変更することです.

たとえば、「Choice 3」をクリックすると、「Text of input 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')

ありがとう :)

編集:実際のコードでいくつかのフォームを使用していることを忘れていました。そのため、他のテキスト入力に対してスクリプトを繰り返さないようにするために、テキスト入力の特定の名前なしでこれを行う方法を探しています。

4

2 に答える 2

1

これを試して:

<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>
于 2009-05-17T07:46:32.483 に答える