Подтвердить что ты не робот

Как передать текст в текстовое поле функции JavaScript?

Предположим, что у меня есть следующий код HTML, как я могу передать пользовательский ввод функции execute (str) JavaScript в качестве аргумента?

<body>

<input name="textbox1" type="text" />
<input name="buttonExecute" onclick="execute(//send the user input in textbox1 to this function//)" type="button" value="Execute" />

</body>
4b9b3361

Ответ 1

Вы можете либо получить доступ к значению элементов по его имени:

document.getElementsByName("textbox1"); // returns a list of elements with name="textbox1"
document.getElementsByName("textbox1")[0] // returns the first element in DOM with name="textbox1"

Итак:

<input name="buttonExecute" onclick="execute(document.getElementsByName('textbox1')[0].value)" type="button" value="Execute" />

Или вы назначаете идентификатор элементу, который затем идентифицирует его, и вы можете получить к нему доступ с помощью getElementById:

<input name="textbox1" id="textbox1" type="text" />
<input name="buttonExecute" onclick="execute(document.getElementById('textbox1').value)" type="button" value="Execute" />

Ответ 2

В отличие от передачи текста как переменной, вы можете использовать DOM для извлечения данных в вашей функции:

var text = document.getElementsByName("textbox1").value;

Ответ 3

Вы можете просто получить входное значение в onclick-событии так:

onclick="execute(document.getElementById('textbox1').value);"

Конечно, вам нужно добавить идентификатор в текстовое поле

Ответ 4

document.getElementById( 'TextBox1'). Значение

Ответ 5

Вот что я сделал. (Адаптируйте все ваши ответы)

<input name="textbox1" type="text" id="txt1"/>
<input name="buttonExecute" onclick="execute(document.getElementById('txt1').value)" type="button" value="Execute" />

Это работает. Всем спасибо.:)

Ответ 6

если я правильно понял вопрос:

<!DOCTYPE HTML>
<HEAD>
<TITLE>Passing values</TITLE>
<style>
</style>
</HEAD>
Give a number :<input type="number" id="num"><br>
<button onclick="MyFunction(num.value)">Press button...</button>
<script>
function MyFunction(num) {
   document.write("<h1>You gave "+num+"</h1>");
}
</script>
</BODY>
</HTML>

Ответ 7

Вы можете получить значение текстового поля и идентификатор на следующем простом примере программирования dotNet

<html>
        <head>
         <script type="text/javascript">
             function GetTextboxId_Value(textBox) 
                 {
                 alert(textBox.value);    // To get Text Box Value(Text)
                 alert(textBox.id);      // To get Text Box Id like txtSearch
             }
         </script>     
        </head>
 <body>
 <input id="txtSearch" type="text" onkeyup="GetTextboxId_Value(this)" />  </body>
 </html>