最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - show form value in alert after click on button - Stack Overflow

programmeradmin1浏览0评论

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....

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

    }  

HTML form

<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

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....

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

    }  

HTML form

<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

Share Improve this question edited Aug 28, 2012 at 9:48 Sandip Armal Patil asked Aug 28, 2012 at 7:40 Sandip Armal PatilSandip Armal Patil 5,90523 gold badges95 silver badges162 bronze badges 1
  • 1 You should call your function like this: updateTxt("select", "textfield"), so in your onclick: onclick='updateTxt("select", "textfield")' – Asciiom Commented Aug 28, 2012 at 7:43
Add a comment  | 

4 Answers 4

Reset to default 5

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);

    }  

You need to pass strings into the updateTxt function. And alert can 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:

JS:

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

    }  ​

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

You will need to change the onClick to:

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.htm You can see there 'select'.

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

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.

Other than that, Amaan's answer should work.

发布评论

评论列表(0)

  1. 暂无评论