如何在输入字段中获取javascript函数参数值

时间:2017-06-13 21:39:45

标签: javascript html

var tbl = document.getElementById("tblMain");
        if (tbl != null) {
            for (var i = 0; i < tbl.rows.length; i++) {
                for (var j = 0; j < tbl.rows[i].cells.length; j++)
                    tbl.rows[i].cells[j].onclick = function () { getval(this); };
            }
        }
 
        function getval(cel) {
            alert(cel.innerHTML);
        }

$("#tblMain  tbody td").on('click', function() {
    $("#empname").val($(this).text());
    
 
$("#empname").on(function () {
    if (this.value.length == this.maxLength) {
      $(this).next('#empname').focus();
    }
});
<table align="center" id="tblMain" border="1" style="cursor: pointer;">
        <tr>
            <td>
                1
            </td>
            <td>
                2
            </td>
            <td>
                3
            </td>
            <td>
                4
            </td>
        </tr>
        <tr>
            <td>
                5
            </td>
            <td>
                6
            </td>
            <td>
                7
            </td>
            <td>
               8
            </td>
        </tr>
        <tr>
            <td>
                9
            </td>
            <td>
                0
            </td>
            <td>
                R3C3
            </td>
            <td>
                R3C4
            </td>
        </tr>
        <tr>
            <td>
                R4C1
            </td>
            <td>
                R4C2
            </td>
            <td>
                R4C3
            </td>
            <td>
                R4C4
            </td>
        </tr>
    </table>
<input type="text" id="empname" maxlength="1">
<input  id="empname2" type="text" maxlength="1" />
cxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdzcxxxxxxxxxxxxxxxxxxxxxxxxfhdfxdzdzdzdzdzdzfdzffzdfzdzdzdzdzdz

2 个答案:

答案 0 :(得分:1)

您缺少一些末端括号和大括号。在调试javascript时,请检查错误控制台(如果在Chrome中按F12键)。这是你的javascript缺少字符:

var tbl = document.getElementById("tblMain");
if (tbl != null) {
    for (var i = 0; i < tbl.rows.length; i++) {
        for (var j = 0; j < tbl.rows[i].cells.length; j++)
            tbl.rows[i].cells[j].onclick = function () { getval(this); };
    }
}

function getval(cel) {
    alert(cel.innerHTML);
}

$("#tblMain  tbody td").on('click', function() {
    $("#empname").val($(this).text());
});

$("#empname").on(function () {
    if (this.value.length == this.maxLength) {
      $(this).next('#empname').focus();
    }
});

你可以诚实地摆脱很多你的Javascript代码。也许你有所有顶级的东西只是为了测试?或者另一种分配onclick处理的形式?你真正需要的只是下半场。 JQuery的东西。虽然我不完全确定你要用第二个做什么。  https://jsfiddle.net/c5n4h9zu/1/

答案 1 :(得分:0)

&#13;
&#13;
var toggle = "#empname";

$("#tblMain  tbody td").on('click', function() {
    switch (toggle) {
    	case "#empname":
      	$(toggle).val($(this).text());
      	toggle = "#empname2";
      	break;
      case "#empname2":
      	$(toggle).val($(this).text());
      	toggle = "#empname3";
      	break;
      case "#empname3":
      	$(toggle).val($(this).text());
      	toggle = "#empname4";
      	break;
      case "#empname4":
      	$(toggle).val($(this).text());
      	toggle = "#empname";
      	break;
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table align="center" id="tblMain" border="1" style="cursor: pointer;">
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
  <tr>
    <td>5</td>
    <td>6</td>
    <td>7</td>
    <td>8</td>
  </tr>
  <tr>
    <td>9</td>
    <td>0</td>
    <td>R3C3</td>
    <td>R3C4</td>
  </tr>
  <tr>
    <td>R4C1</td>
    <td>R4C2</td>
    <td>R4C3</td>
    <td>R4C4</td>
  </tr>
 </table>
 
<input type="text" id="empname" value=""/>
<input type="text" id="empname2" value=""/>
<input type="text" id="empname3" value=""/>
<input type="text" id="empname4" value=""/>
&#13;
&#13;
&#13;

你可以做什么(如果我正确理解你的问题),为每个号码添加onclick事件处理程序。他们运行的代码会将输入的.value属性设置为按下的相应数字。希望这会有所帮助。