关于如何动态获取button的value值

FengHanWei 2013-11-05 08:40:27
怎么实现让h1的value值等于第一个被点的按钮
让h2的value值等于第二个被点的按钮
让h3的value值等于第三个被点的按钮的getvalue()函数可以加参数。。初学者。。不会


<body>
<form name="form1" method="post" action="myb.jsp">
<input type="button" name="1" id="1" value="1" onclick="getvalue()"/>
<input type="button" name="2" id="2" value="2" onclick="getvalue()"/>
<input type="button" name="2" id="3" value="3" onclick="getvalue()"/>
<input type="submit" name="=" id="="/>
<input type="hidden" name="h1" id="h1" value="4"/>
<input type="hidden" name="h2" id="h2" value="5"/>
<input type="hidden" name="h3" id="h3" value="6"/>

<script>
function getvalue(btn){



}
</script>
</form>
</body>
...全文
1652 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Adronfan 2013-11-06
  • 打赏
  • 举报
回复
总体思路就是用javascript,先获取到form,在获取到form里面的button,在获取到button上面的按钮,javascript获取页面值这样的思路最基础,还有就是使用id或者name,多试试就ok了,祝福你,
坂田银时123 2013-11-05
  • 打赏
  • 举报
回复
看得有点晕,不过看懂了
FengHanWei 2013-11-05
  • 打赏
  • 举报
回复
赶脚是这样了。。试下先。。可不可以加扣扣哦~~
醤油様 2013-11-05
  • 打赏
  • 举报
回复

<body>
<form name="form1" method="post" action="myb.jsp">
<input type="button" name="1" id="1" value="1" onclick="getvalue(this,'h1')"/>
<input type="button" name="2" id="2" value="2" onclick="getvalue(this,'h2')"/>
<input type="button" name="2" id="3" value="3" onclick="getvalue(this,'h3')"/>
<input type="submit" name="=" id="="/>
<input type="hidden" name="h1" id="h1" value="4"/>
<input type="hidden" name="h2" id="h2" value="5"/>
<input type="hidden" name="h3" id="h3" value="6"/>

<script>
var count=1;
function getvalue(btn){
	document.getElementById("h"+count).value=btn.value;
	
	alert(document.getElementById("h"+count).value);
	count++;
	if(count==4){
		count=1;
	}
}
</script>
</form>
</body>
好像明白你要的效果了,改了下,如果点了3次后又重新从第1次开始,看看是不是这样
FengHanWei 2013-11-05
  • 打赏
  • 举报
回复
我的意思是不知道第一个按钮是哪个。。按钮随机按~~有可能相同
引用
醤油様 2013-11-05
  • 打赏
  • 举报
回复

<body>
<form name="form1" method="post" action="myb.jsp">
<input type="button" name="1" id="1" value="1" onclick="getvalue(this,'h1')"/>
<input type="button" name="2" id="2" value="2" onclick="getvalue(this,'h2')"/>
<input type="button" name="2" id="3" value="3" onclick="getvalue(this,'h3')"/>
<input type="submit" name="=" id="="/>
<input type="hidden" name="h1" id="h1" value="4"/>
<input type="hidden" name="h2" id="h2" value="5"/>
<input type="hidden" name="h3" id="h3" value="6"/>

<script>
function getvalue(btn,h){
	document.getElementById(h).value=btn.value;
	alert(document.getElementById(h).value);
}
</script>
</form>
</body>
稍微改了下,纯js的,JS熟悉了可以学学jquery,比较流行

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧