html5中如何通过点击按钮 自动填写表单

一共有3中填写模板,点第一个按钮,表单自动填写为第一种,点第二个按钮,表单自动填写为第二种,以此类推。求解!!!


森栏
浏览 949回答 1
1回答

不负相思意

可以写一个为表单内元素赋值的方法,三个按钮上面添加点击事件,点击时调用这个赋值方法,但是传的参数不一样,在方法中根据不同的参数,为表单赋不同模板的值。不知上面是否写的清楚,我写了一个对应的小demo:<html><head><title>demo</title><script type="text/javascript" src="jquery-1.8.3.js"></script><script type="text/javascript">&nbsp; &nbsp; function demo(demo){&nbsp; &nbsp; &nbsp; &nbsp; if(demo ==1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#input1").attr("value","这是模板1");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#input2").attr("value",demo);&nbsp; &nbsp; &nbsp; &nbsp; } else if(demo ==2){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#input1").attr("value","这是模板2");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#input2").attr("value",demo);&nbsp; &nbsp; &nbsp; &nbsp; } else if(demo ==3){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#input1").attr("value","这是模板3");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#input2").attr("value",demo);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }</script></head><body>&nbsp; &nbsp; <button onclick="demo(1)">模板1</button>&nbsp; &nbsp; <button onclick="demo(2)">模板2</button>&nbsp; &nbsp; <button onclick="demo(3)">模板3</button>&nbsp; &nbsp; <form>&nbsp; &nbsp; &nbsp; &nbsp; <input id="input1" type="text"/>&nbsp; &nbsp; &nbsp; &nbsp; <input id="input2" type="text"/>&nbsp; &nbsp; </form></body></html>以上,希望对你有帮助!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5