weixin_33747129 2009-12-21 22:46 采纳率: 0%
浏览 30

jQuery的每个CSS数组

I have made an each statement which iterates through each class item found. At the same time there is another item with a different class but has the same key. I am needing to get the key from the first each and put it into the 2nd item without having to loop a second time.

$('.searchby').each(function(k,v){ // looping through .searchby
    $(v).live('change',function(){ // when .searchby drop down changed do this
        selv = $(this).val(); // get value of drop down
        $.post('file.php',{ 'val' : selv },function(data){

            // Below is where I need what I need to happen.
            // Previously I had .searchby as id but need multiple.
            // So, what I need it to do is like php.
            // $('.searchfield[k]).html(data); // <-- like this

            $('#searchfield').html(data);


        },'html'); // html, xml, script, json
    });
});

--EDIT--

First, the v in $(v).live should be $(this).live 2nd, I am wanting to convert the single/id pull script to a multi pull script. So, I wrote this which works.

$('.searchby').each(function(k,v){
    $(this).change(function(){
        selv = $(this).val();
        $.post('file.php',{
            'val' : selv
        },function(data){
            $('.searchfield').each(function(kk,vv){
                if(k == kk) $(vv).html(data);
            });
            //$('#searchfield').html(data);
        },'html'); // html, xml, script, json
    });
});

But I would like to be able to take the inner .each out and replace it with:

$('.searchfield:eq('+k+')') <-- something like this if I can get it to work

So the solution is this:

$('.searchby').each(function(k,v){
    $(this).change(function(){
        selv = $(this).val();
        $.post('file.php',{
            'val' : selv
        },function(data){
            $('.searchfield:eq('+k+')').html(data);
        },'html'); // html, xml, script, json
    });
});

Thanks everyone

  • 写回答

2条回答 默认 最新

  • Memor.の 2009-12-21 22:54
    关注

    The question is not clear. You talk of classes, but then your code uses $('#searchfield'), which is used to select elements with the given ID.

    Assuming you really want to select elements with a given class (which is more correct, as there should not be elements with duplicated ID), and if there are exactly one element with class searchfield for each elements selected by $.each(), then you can use $($('.searchfield')[k]).html(data).

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3