获取button中value值的方法

2025-10-06 02:23:24

//第一种方式,在button标签添加onclick事件,点击按钮触发函数获取button中的value值

function getdata(){

var str=document.getElementById("iupt");

alert(str.value);

}

//第二种方法:先通过id用getElementById获取到button元素,直接在其后添加onclick点击事件,再在其后定义函数,在函数中写入函数体

document.getElementById("btn1").οnclick=function getdata(){

var str=document.getElementById("btn1");

alert(str.value);

}

//第三种方式:先通过id用getElementById获取到button元素,直接用onclick调用已经定义好的函数

function getdata(){

var str = document.getElementById("iupt");

alert(str.value);

}

document.getElementById("btn2").οnclick=getdata;

//第四种方式:先通过id用getElementById获取到button元素,在其后添加一个事件,调用定义好的方法

//addEventListener("触发方式","所要触发的方法");

document.getElementById("btn3").addEventListener("click",getdata);

//第五种方式:先用getElementById获取到按钮ID,在其后添加一个事件,在事件中直接定义方法,写入方法体(常用)

document.getElementById("btn4").addEventListener("click",function getdata(){

var str=document.getElementById("iupt");

alert(str.value);

————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/qq_36930221/article/details/79872083