关于tr周围缺少表格标签的怪异jQuery功能 [英] Weird jQuery functionality on missing table tags around tr

查看:131
本文介绍了关于tr周围缺少表格标签的怪异jQuery功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看以下我没有用表标签自动包装的html:

Check out the below html which I haven't wrapped with table tags knowingly:

html:

<tr class="item1">
    <td>
        <select name="postArrayFormato[]" id="test">
             <option value="frascoG">Frasco 370g</option>
         <option value="frascoC">Frasco 220g</option>
         <option value="sachet">Sachet 200g</option>
         <option value="doypack">Doy/Pack 250g</option>
        </select>
    </td>
    <td><input type="text" name="test" value="20" readonly/></td>
</tr>

jquery:

由于某种原因,这与上面的html一起正常工作,对我来说似乎很奇怪.

This works fine with the above html for some reason, which looks weird to me.

js小提琴:

http://jsfiddle.net/gM2vX/5/

$(function(){
    $("body").on("change","#test",function(){
        $(this).next().val("99");
    });
});

当我在tr周围添加表格标签时,上面的jquery不起作用,这很明显,它需要以下代码:

The above jquery doesn't work when I add table tags around the tr, which is obvious and it needs the below code:

js小提琴: http://jsfiddle.net/gM2vX/4/

$(function(){
    $("body").on("change","#test",function(){
        $(this).parent().next().find("input").val("99");
    });
});

但是我的问题为什么在第一种情况下可以正常工作? 这不是问题,只是需要澄清.

But my question why is it working fine in the first case? This is not an issue but just need clarity.

推荐答案

检查页面的html.由于您不能拥有表中没有的tr,因此浏览器通过删除trtd元素为您修复"它,结果是删除select成为兄弟姐妹:

Inspect the html of the page. Since you can't have a tr that isn't in a table, the browser "fixes" it for you by removing the tr and td elements, and as a result, the select and input become siblings:

<body style=""> 
    <select name="postArrayFormato[]" id="test">
        <option value="frascoG">Frasco 370g</option>
        <option value="frascoC">Frasco 220g</option>
        <option value="sachet">Sachet 200g</option>
        <option value="doypack">Doy/Pack 250g</option>
    </select>
    <input type="text" name="test" value="20" readonly="">
</body>

这篇关于关于tr周围缺少表格标签的怪异jQuery功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆