jQuery,读取表单内所有button的id,谢谢

怎样用j query读取表单内所有button的id值,然后在给button自动赋一个click的动作。

$("#form1").find("button").each(function () {

//方法体
})

$("input[id='button']").click(function(){ alert("....");});

$("form input"),这个是from表单中所有input标签选择器,你可以试试一个var 接收哪个什么do-----ById,读法应该是“多球门特白ID”,我很少用所以不记得的
,遍历var,在循环里给一个attr添加事件

使用jQuery查找所有的按钮,使用on() 方法在被选元素及子元素上添加一个或多个事件处理程序

 //按照jquery方式添加事件处理2  
        function BindByJquery2() {  
            $('button').on("click", function () {  
                try{  
                    var uid = $(this)[0].getAttribute('uid');  
                    alert("lenght = " + $(this).length + "点击了按钮,uid = " + uid);  
                }  
                catch (error) {  
                    alert(error);  
                }  
            });  

        }  
        //按照jquery方式添加事件处理3  
        function BindByJquery3() {  
            try{  
                $(document).on("click", "button", function () {  
                    var uid = this.getAttribute('uid');  
                    alert("lenght = " + $(this).length + "点击了按钮,uid = " + uid);  
                });  
                $(document).on("click", "img", function () {  
                    //alert("img");  
                    win = window.open("http://blog.csdn.net/mfcing", "", "width=200,height=200");  

                    //OpenWindow();  
                });  
            }  
            catch (error) {  
                alert(error);  
            }  
        }  

建议利用事件委托,在某个父节点绑定点击事件。判断点击的源为button的话则处理

把jquery路径换一下,在console.log里面查看;


1
2
3


<br> $(&quot;button&quot;).each(function(index){<br> console.log($(this).attr(&quot;id&quot;));<br> $(this).click(function(){<br> console.log(&quot;11&quot;);<br> })<br> })<br>

$("input[type='button']").click(function(){
//动作
})
var buttons = $("input[type='button']");
for(var i=0 ; i< buttons.length ; i++){
let id = buttons.get(i).getAttribute("id");
}

上面复制下来变了,又重新手写了,希望对你有帮助;打开检查,在console里面查看
123
$("button").each(function(index){
console.log($(this).attr("id")); $(this).click(function(){ console.log("11"); })
})