点击传值不会懂写循环,路过的大大帮个忙

https://img3.mukewang.com/5cd91277000166e107370409.jpg

 <!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>javascript点击输入</title>
</head>
<body>
<style type="text/css">
a{text-decoration:none;color:#2d374b;}
table.FormTable{ width:100%}
table.FormTable tr td{padding:12px 0px}
table.FormTable tr td.tdL{width:12%;} 
table.FormTable tr td.tdC{width:88%;}
table.FormTable tr td.tdR{width:18%; border-left:inset 1px #eee}
input.srsz{ width:62px; height:30px; float:left; margin-left:5px; text-align:center;
margin-bottom:10px; font-size:18px;color:#33F}
td.sz0_9 a{width:36px; height:38px; line-height:38px; float:left; margin-right:3px; 
font-size:16px;text-align:center; background-color:#CCC}
</style>
<div class="MainContainers" style="width:600px; height:600px">
<form name="" method="post" action="" class="layui-form ">
  <div class="Conter">  
   <table class="FormTable" style=" padding:0">
    <tr id=""> 
      <td class='tdL'>   
      </td>
      <td class="tdC" style=" height:80px">
         <div class="" style="  float:left;">       
           <input type='text' name='post_db[sz_a1]' id='sz_a_1' onClick="javascript:;" class="srsz" value=""/>
           <input type='text' name='post_db[sz_a2]' id='sz_a_2' onClick="javascript:;" class="srsz" value=""/>
           <input type='text' name='post_db[sz_a3]' id='sz_a_3' onClick="javascript:;" class="srsz" value=""/>
           <input type='text' name='post_db[sz_a4]' id='sz_a_4' onClick="javascript:;" class="srsz" value=""/>           
           <input type='text' name='post_db[sz_a5]' id='sz_a_5' onClick="javascript:;" class="srsz" value=""/>
           <input type='text' name='post_db[sz_a6]' id='sz_a_6' onClick="javascript:;" class="srsz" value=""/>              
         </div>  
      </td>
    </tr>       
    <tr>


慕神8447489
浏览 541回答 7
7回答

慕运维8079593

<!DOCTYPE&nbsp;html> <html> <head> &nbsp;&nbsp;<meta&nbsp;charset="utf-8"> &nbsp;&nbsp;<title>javascript点击输入</title> </head> <body> <style&nbsp;type="text/css"> a{text-decoration:none;color:#2d374b;} table.FormTable{&nbsp;width:100%} table.FormTable&nbsp;tr&nbsp;td{padding:12px&nbsp;0px} table.FormTable&nbsp;tr&nbsp;td.tdL{width:12%;}&nbsp; table.FormTable&nbsp;tr&nbsp;td.tdC{width:88%;} table.FormTable&nbsp;tr&nbsp;td.tdR{width:18%;&nbsp;border-left:inset&nbsp;1px&nbsp;#eee} input.srsz{&nbsp;width:62px;&nbsp;height:30px;&nbsp;float:left;&nbsp;margin-left:5px;&nbsp;text-align:center; margin-bottom:10px;&nbsp;font-size:18px;color:#33F} td.sz0_9&nbsp;a{width:36px;&nbsp;height:38px;&nbsp;line-height:38px;&nbsp;float:left;&nbsp;margin-right:3px;&nbsp; font-size:16px;text-align:center;&nbsp;background-color:#CCC} </style> <div&nbsp;class="MainContainers"&nbsp;style="width:600px;&nbsp;height:600px"> <form&nbsp;name=""&nbsp;method="post"&nbsp;action=""&nbsp;class="layui-form&nbsp;"> &nbsp;&nbsp;<div&nbsp;class="Conter">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<table&nbsp;class="FormTable"&nbsp;style="&nbsp;padding:0"> &nbsp;&nbsp;&nbsp;&nbsp;<tr&nbsp;id="">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<td&nbsp;class='tdL'>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<td&nbsp;class="tdC"&nbsp;style="&nbsp;height:80px"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div&nbsp;class=""&nbsp;style="&nbsp;&nbsp;float:left;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input&nbsp;type='text'&nbsp;name='post_db[sz_a1]'&nbsp;id='sz_a_1'&nbsp;onClick="targetObj=this;"&nbsp;class="srsz"&nbsp;value=""/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input&nbsp;type='text'&nbsp;name='post_db[sz_a2]'&nbsp;id='sz_a_2'&nbsp;onClick="targetObj=this;"&nbsp;class="srsz"&nbsp;value=""/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input&nbsp;type='text'&nbsp;name='post_db[sz_a3]'&nbsp;id='sz_a_3'&nbsp;onClick="targetObj=this;"&nbsp;class="srsz"&nbsp;value=""/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input&nbsp;type='text'&nbsp;name='post_db[sz_a4]'&nbsp;id='sz_a_4'&nbsp;onClick="targetObj=this;"&nbsp;class="srsz"&nbsp;value=""/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input&nbsp;type='text'&nbsp;name='post_db[sz_a5]'&nbsp;id='sz_a_5'&nbsp;onClick="targetObj=this;"&nbsp;class="srsz"&nbsp;value=""/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input&nbsp;type='text'&nbsp;name='post_db[sz_a6]'&nbsp;id='sz_a_6'&nbsp;onClick="targetObj=this;"&nbsp;class="srsz"&nbsp;value=""/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td> &nbsp;&nbsp;&nbsp;&nbsp;</tr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<tr> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<td&nbsp;&nbsp;class='tdL'></td>&nbsp;<td&nbsp;class="sz0_9"&nbsp;> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:ToSa('1')">1</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:ToSa('2')">2</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:ToSa('3')">3</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:ToSa('4')">4</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:ToSa('5')">5</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:ToSa('6')">6</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:ToSa('7')">7</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:ToSa('8')">8</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:ToSa('9')">9</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:ToSa('0')">0</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a&nbsp;href="javascript:delpic()">X</a> <SCRIPT&nbsp;LANGUAGE="JavaScript"> var&nbsp;targetObj&nbsp;=&nbsp;document.getElementById("sz_a_1"); function&nbsp;ToSa(va){ &nbsp;&nbsp;&nbsp;&nbsp;targetObj.value+=va; } function&nbsp;delpic(){ &nbsp;&nbsp;&nbsp;&nbsp;targetObj.value='';&nbsp; } </SCRIPT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td> &nbsp;&nbsp;&nbsp;&nbsp;</tr>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</table> </div> </form> </div> </body> </html>

温温酱

这个function改造下:function ToSa(va){var ctrl = ('input:focus');cc=$(ctrl).val();if(cc==''){$(ctrl).val(va);}else{$(ctrl).val(cc+va);}}

有只小跳蛙

修改ToSa函数var index=1;function ToSa(va){var cc=document.getElementById("sz_a_"+index++)if(cc.value==''){cc.value=va;}},还有你的那个x按钮时什么功能,不说不好改

HUX布斯

最好把input的onClick事件改成onfocus

四季花海

获取当前获得焦点的input<!DOCTYPE html>javascript点击输入a{text-decoration:none;color:#2d374b;} table.FormTable{ width:100%} table.FormTable tr td{padding:12px 0px} table.FormTable tr td.tdL{width:12%;} table.FormTable tr td.tdC{width:88%;} table.FormTable tr td.tdR{width:18%; border-left:inset 1px #eee} input.srsz{ width:62px; height:30px; float:left; margin-left:5px; text-align:center; margin-bottom:10px; font-size:18px;color:#33F} td.sz0_9 a{width:36px; height:38px; line-height:38px; float:left; margin-right:3px; font-size:16px;text-align:center; background-color:#CCC}1&nbsp;2&nbsp;3&nbsp;4&nbsp;5&nbsp;6&nbsp;7&nbsp;8&nbsp;9&nbsp;0&nbsp;X&nbsp;var index=1; var inputs=document.getElementsByTagName('input'); for(var i = 0; i < inputs.length; i++){ inputs[i].onfocus=function(i) { getIndex(); } } function getIndex() { for(var i=0;i<inputs.length;i++){ if(inputs[i]==document.activeElement){ index=i+1; } } } function ToSa(va){ cc=document.getElementById("sz_a_"+index).value if(cc==''){ document.getElementById("sz_a_"+index).value=va; }else{ document.getElementById("sz_a_"+index).value+=+va; } } function delpic(t){ document.getElementById("sz_a_"+index).value=''; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript