1

OK、これはおそらくこれまでで最もばかげた質問ですが、ご容赦ください....

これを機能させる方法:

$("#basephoto").after(
  '<tr><td valign="bottom">Additional photo:</td>
       <td>&nbsp;&nbsp;&nbsp;</td>
       <td><div id="addphoto'+curupfld+'" class="browsebg">
         <p class="bpath"></p>
         <input onchange="fillBrowse(\"#addphoto'+curupfld+'\",this); validateExt(field);"  class="browse transfield" type="file" name="altphoto'+curupfld+'" size="40" />
         <span onclick="delImgField(this.id)" id="delbtn'+curupfld+'" class="abuttons delbtn"></span></div>
       </td>
   </tr>');

関心のある部分:

onchange="fillBrowse(\"#addphoto'+curupfld+'\",this); validateExt(field);"

問題は「onchange」から始まります。これら2つを呼び出す関数をいつでも作成でき、解決策は次のようになります。

$("#basephoto").after(
  '<tr>
     <td valign="bottom">Additional photo:</td>
     <td>&nbsp;&nbsp;&nbsp;</td>
     <td>
       <div id="addphoto'+curupfld+'" class="browsebg">
       <p class="bpath"></p>
       <input onchange=functionCaller("#addphoto'+curupfld+'",this) class="browse transfield" type="file" name="altphoto'+curupfld+'" size="40" />
       <span onclick="delImgField(this.id)" id="delbtn'+curupfld+'" class="abuttons delbtn"></span>
       </div>
      </td>
   </tr>');

これは機能しますが、可能であれば、回避策を使用するのではなく、問題を解決したいと考えています。

4

2 に答える 2

2

私はあなたが本当に何をしているのか本当に知りません。しかし、それをよりjQueryの方法で書きたくない場合..これは役立つかもしれません..

$('#basephoto').append('<tr />');
$('#basephoto tr').append(
    $('<td />')
        .attr('valign','bottom')
        .html('Additional photo:')
);
$('#basephoto tr').append(
    $('<td />')
        .html('&nbsp;&nbsp;&nbsp;')
);
$('#basephoto tr').append(
    $('<td />')
        .html(
            $('<div />')
            .attr({
                "id":"addphoto-"+curupld,
                "class":"browsebg"  
            })
            .html(
                $('<p />')
                    .attr('class','bpath')
                    .html('')
            )
        )
);
$('#addphoto-'+curupfld).append(
    $('<input />')
        .attr({
            "class":"browse transfield",
            "type":"file",
            "name":"altphoto-"+curupfld,
            "size":"40"
        })
        .change(function(){
            functionCaller('#addphoto-'+curupfld,this);
            validateExt(field);
        })
);
$('#addphoto-'+curupfld).append(
    $('<span />')
        .attr({
            "class":"abuttons delbtn",
            "id":"delbtn-"+curupfld
        })
        .click(function(){
            delImgField($(this).attr('id'));
        })
);

jQuery を使用して要素を作成する方法について詳しくは、この投稿をご覧ください。

問題を解決するには、次のような行を追加するだけでよいと思います。

$('#input_that_has_the_change_event').change(function(){
    fillBrowse('#addphoto'+curupfld,this);
    validateExt(field);
});

*PS: ばかげているとは言わないでください。私は以前のあなたと同じです.. :]

于 2012-03-30T22:31:53.493 に答える
0

アンダースコアのようなテンプレートシステムを使用します。保守可能で、読みやすく、トラブルシューティングも簡単です。

http://documentcloud.github.com/underscore/

例:

    <script id="templateA" type="text/template">
          <p>
                 Test stuff <a href="/home.html"><%= label %></a>
                 ...
          </p>
    </script>
...

<script type="javascript">
        var templateFunction = _.template($('#templateA').html());
        $('#someDivID').after(templateFunction({label: 'Here'}));
</script>
于 2012-03-30T21:54:34.680 に答える