开发者

Fill Form by Clicking Text Link?

I need a code like this. This is an example but it is not 开发者_Go百科working to IE7

http://fiddle.jshell.net/Pc4uT

Theese are my links :

  1. Link 1
  2. Link 2
  3. Link 3
  4. Link 4
  5. Link 5

it must filling to input field with link value when click to links. Like this : 1,2,3,4,5

Can someone help me for this problem ?


Use jQuery's .click( instead of attr("onclick", ... To be compatible with different browsers and don't return a string of javascript. You should avoid putting anything that looks like scripting inside a string. Use this:

$("a.mylink").each(function(i){
    $(this).click(function(){
         document.getElementById('row1').value += i + ',';
    }); 
});


http://fiddle.jshell.net/Pc4uT/37/ see here it works

You need to use click instead of attr

$("a.mylink").click(function (arr) {
    if($('#row1').val()==''){
        $('#row1').val( $(this).attr('name'));
    }else{
      $('#row1').val( $('#row1').val()+','+$(this).attr('name'));
    }
}); 

and you can use name in link to kee the link number

<a class="mylink" name="1">First Link</a>     
    <a class="mylink" name="2">Second Link</a>


You could store the value in a data attribute for each link:

<a href="#" data-val="1" class="link">Link 1</a>
<a href="#" data-val="2" class="link">Link 2</a>
etc.

Then you could retrieve the value and insert it into the input:

$('.link').click(function(){
    $('#input_id').val($('#input_id').val()+', '+$(this).data('val'));
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜