Javascript 单击按钮后在警报中显示表单值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12154786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:15:10  来源:igfitidea点击:

show form value in alert after click on button

javascripthtmlbuttonalert

提问by Sandip Armal Patil

I am very new on HTML. I just trying to accept form value and want to show this value
in Alert. I try following code but it didn't help me...
I know this is very easy question but i am newbie on HTML,JavaScript. I search but didn't find relative to my requirement.... Javascript function....

我对 HTML 很陌生。我只是想接受表单值并希望
在警报中显示此值。我尝试以下代码,但它没有帮助我...
我知道这是一个非常简单的问题,但我是 HTML、JavaScript 的新手。我搜索但没有找到相对于我的要求.... Javascript 功能....

  function updateTxt(field1,field2)
  {  
    var field1 = document.getElementById(field1);  
    var field2 = document.getElementById(field2);  
    alert(field1,field2);

    }  

HTML form

HTML 表单

<input name="textfield" type="text" id="textfield" value="Search by keyword" class="search_input" >
<select name="select" id="select" class="input_list" >
          <option>Search jobs by category</option>
          <option>Business Sales Leadership Development Program</option>
          <option>Business Sales Solutions</option>
          <option>CTO</option>
          <option>Call Center</option></select>
 <input name="button" type="button" class="btn" value="Go" onClick="updateTxt(select,textfield)">

Please give me hint or direct me where i wrong...
Thanks in Advance

请给我提示或指导我哪里错了......
提前致谢

回答by Naresh Tank

change this code

更改此代码

<input name="button" type="button" class="btn" value="Go" onclick="updateTxt()">

and also this

还有这个

     function updateTxt()
  {  
    var field1 = document.getElementById('textfield').value;  
    var field2 = document.getElementById('select').options[document.getElementById('select').selectedIndex].value;  
    alert(field1+'&'+field2);

    }  

回答by Some Guy

You need to pass strings into the updateTxtfunction. And alertcan only take one parameter, so you'll have to concatenate the values of the fields, not the fields themselves. Your code would look like this:

您需要将字符串传递给updateTxt函数。并且alert只能采用一个参数,因此您必须连接字段的值,而不是字段本身。您的代码如下所示:

JS:

JS:

function updateTxt(field1,field2)
  {  
    var field1 = document.getElementById(field1).value;  
    var field2 = document.getElementById(field2).value;  
    alert(field1 + '\n' + field2);

    }  ?

HTML:

HTML:

<input name="textfield" type="text" id="textfield" value="Search by keyword" class="search_input" >
<select name="select" id="select" class="input_list" onkeyup="updateTxt('select','txt2');">
          <option>Search jobs by category</option>
          <option>Business Sales Leadership Development Program</option>
          <option>Business Sales Solutions</option>
          <option>CTO</option>
          <option>Call Center</option></select>
 <input name="button" type="button" class="btn" value="Go" onClick="updateTxt('select', 'textfield')">?

Demo

演示

回答by Ido Green

You will need to change the onClick to:

您需要将 onClick 更改为:

onclick="updateTxt('select','textfield');

because when you are using reserved words in JS it is not good - to say the least ;) For full list of reseved words: http://www.javascripter.net/faq/reserved.htmYou can see there 'select'.

因为当您在 JS 中使用保留字时,这并不好 - 至少可以这么说 ;) 有关保留字的完整列表:http: //www.javascripter.net/faq/reserved.htm您可以在那里看到“选择”。

Btw, you might want to change the names to something better and avoid 'onClick' and JS inside your html. Good luck.

顺便说一句,您可能希望将名称更改为更好的名称,并避免在 html 中使用“onClick”和 JS。祝你好运。

回答by Endy

Your code does not have an element with the id 'txt2'. Also try to use more explicit IDs, like 'myTextField' instead of 'select', it will make it easier to read and maintain your code.

您的代码没有 ID 为“txt2”的元素。还要尝试使用更明确的 ID,例如“myTextField”而不是“select”,这样可以更轻松地阅读和维护您的代码。

Other than that, Amaan's answer should work.

除此之外,Amaan 的答案应该有效。