简体   繁体   English

如何使用Java脚本从文本框中获取文本的值

[英]How to get the value of text from text boxes using Java Script

I have a requirement where text boxes, which are generated dynamically, display some data on the screen. 我要求动态生成的文本框在屏幕上显示一些数据。
The text boxes are editable and if users want to change the content, they can put the enter new data and push it on the server. 文本框是可编辑的,并且如果用户要更改内容,则可以将输入的新数据放入服务器中。
I tried sending the data using Forms, however, it is not working, so have to resort to Java Script by capturing the data on client side and then pushing it to the server. 我尝试使用Forms发送数据,但是它不起作用,因此必须通过在客户端捕获数据然后将其推送到服务器来诉诸Java Script。

The problem that I'm facing is that the text boxes are generated dynamically and have same ids and tag names. 我面临的问题是文本框是动态生成的,并且具有相同的ID和标记名称。
I'm able to find the count of the text boxes, however not able to find the value of the text boxes. 我能够找到文本框的数量,但是无法找到文本框的值。 Can some one please help. 有人可以帮忙吗?

My HTML code is 我的HTML代码是

while($row = $result->fetch_assoc()) 
{
    $fieldName[] = $row['fieldName'];
    $fieldText[] = $row['fieldText'];
    $fieldID[]   = $row['ID'];

    if ($_GET['showname']){                                 
        echo"<tr>";
        echo "<td>".$row['fieldName']."</td>";
        echo '<td>'.'<input type = "text" class="form-control" disabled = "disabled" id ="fieldText" name = "fieldText['.$row["ID"].']" value = "'.$row["fieldText"].'">'."</td>";
        //echo "<td>".$row['fieldText']."</td>";
        echo "</tr>";
    }

On submit button, I'm calling the Update function that will capture the run time values in the text boxes: 在提交按钮上,我正在调用Update函数,该函数将捕获文本框中的运行时值:

function updateVal()
{
    var node_list = document.getElementsByTagName('input');
    var c=0;
    for (var i = 0; i < node_list.length; i++) {
        var node = node_list[i];
        if (node.getAttribute('type') == 'text')
        {

        //Not sure how to extract the value from Text boxes and create a json
         c++;
        }
    }
}

Something like this: 像这样:

var capturedValues = {};

Then in your forloop do: 然后在您的forloop中执行:

capturedValues[node.name] = node.value;

Finally convert object to JSON string: 最后将对象转换为JSON字符串:

var jsonStr = JSON.stringify(capturedValues);

I have created three arrays as below. 我创建了以下三个数组。

 var fieldName = [];
 var fieldText = []
 var ID = [];

And constructed a json object postData and stringified the same. 并构造了一个json对象postData并对其进行了字符串化。

Sample : 样品:

 function updateVal() { var node_list = document.getElementsByTagName('input'); var c = 0; var fieldName = []; var fieldText = [] var ID = []; for (var i = 0; i < node_list.length; i++) { var node = node_list[i]; if (node.getAttribute('type') == 'text') { fieldName[c] = node.name; fieldText[c] = node.value; ID[c] = node.id; c++; } } var postData = { fieldName: fieldName, fieldText: fieldText, ID: ID }; console.log(JSON.stringify(postData)); } updateVal(); 
 <input type="text" id="text1" name="text1" value="text1value" /> <input type="text" id="text2" name="text2" /> <input type="text" id="text3" name="text3" /> <input type="text" id="text4" name="text4" value="text4value" /> 

JSFiddle 的jsfiddle

   <input type="text" id="myText" value="">
   <p id="out"></p>
   <button onclick="myFunction()">Try it</button>
    <script>
         function myFunction() {
           var txt= document.getElementById("myText").value;
           document.getElementById("out").innerHTML=txt;}
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM