前端: jquery绑定/动态变量/find/each/

目标与html代码

目标:点击jq删除,获取整行tr的id,(后端部分略,传递id处理即可),页面删除该tr行

  <tr myid="{{ cls.id }}">
     ....

      <td><a onclick="rmStu(this);" class="abc" href="#">jq删除</a></td>
【方式1】由onclick触发,点击位置由this传递
动态变量由字符串拼接
<script>
    function rmStu(th) {
    var myid=$(th).parent().parent().attr('myid');
    alert(myid);
{#    $(th).parent().parent().remove();#}
{#    $("[myid='11']").remove();#}
{#    $("[myid=myid]").remove();无法定位#}
    $("[myid="+myid+"]").remove();

    }
</script>
【方式2】事件绑定。点击触发,条件为任意class="abc"时。
触发时再由$(this)获取点击位置
<script>
    $(function () {
        bindEvent();
    });
    function bindEvent() {
        $('.abc').click(function () {
            var myid=$(this).parent().parent().attr('myid');
            alert(myid);

        })
    }

</script>
附录 bootstrap modal show
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/plugin/bootstrap-3.3.7/js/bootstrap.js"></script>
<script>
    $(function () {
        abc();
    });
    function abc() {
        $('#showmyModal').click(function () {
            $('#myModal').modal('show');
        })
    }

</script>
附录ajax
            $.ajax({
                url: '/ajax_deal/',
                type: 'POST',
                data: { var1: var1, var2:var2},
                success:function (arg) {
                    $('#id1').val(arg);
                }
            })

多属性查找
找到每个input整行

    var aaa = $("div[name='nam1'][attr1='XXX']").find("input[type='boy'][name='studentname']");
    console.log(aaa.val()); 
  $('xx').find('input').each(function () {
            console.log(this);
            console.log("####")
        })

找到每个行与数据,保存待用。 .find('a,b,c')可同时找多种

<script>
    $(function () {
        bindsth();
    });
    function bindsth() {
        var datadict={};
        $("div[name='xx'][school='xx']").find("input").each(function () {
            console.log(this);
            console.log("####");
            var value =$(this).val();
            var name=$(this).attr('name');
            datadict[name]=value;
        });
     }
</script>

checked无法用attr获取。用prop。
在jQuery 1.6及以后版本中,使用prop()函数来设置或获取checked、selected、disabled等属性。对于其它能够用prop()实现的操作,也尽量使用prop()函数。
布尔类型:attr 自定义值,html默认值。 实时值用prop
$('xx').prop('checked')
$('xx').prop('checked',True)

            if ($('input').prop('checked')) {
                $('input').prop('checked',false);
            }else{
                $('input').prop('checked',true);
            }

清空
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');


$('#myform')[0].reset();

转载于:https://blog.51cto.com/13606158/2074340

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值