如何在JavaScript中获取文本框值

时间:2023-01-28 17:59:22

I am trying to use JavaScript to get the value from an HTML text box but value is not coming after white space

我正在尝试使用JavaScript从HTML文本框中获取值,但是值不会在空格后出现

For example:

例如:

<input type="text" name="txtJob" value="software engineer">

I only get: "software" from the above. I am using a script like this:

我只从上面得到:“软件”。我使用的脚本是这样的:

var jobValue = document.getElementById('txtJob').value

How do I get the full value: "software engineer"?

如何获得“软件工程师”的全部价值?

15 个解决方案

#1


55  

+1 Gumbo: ‘id’ is the easiest way to access page elements. IE (pre version 8) will return things with a matching ‘name’ if it can't find anything with the given ID, but this is a bug.

+1 Gumbo:“id”是访问页面元素的最简单方法。IE (pre - version 8)将返回与给定ID匹配的“name”,但这是一个bug。

i am getting only "software".

我只得到“软件”。

id-vs-name won't affect this; I suspect what's happened is that (contrary to the example code) you've forgotten to quote your ‘value’ attribute:

id-vs-name不会影响;我怀疑发生了什么(与示例代码相反),您忘记引用您的“value”属性:

<input type="text" name="txtJob" value=software engineer>

#2


71  

Your element does not have an ID but just a name. So you could either use getElementsByName() method to get a list of all elements with this name:

元素没有ID,只有名称。因此,您可以使用getElementsByName()方法获取所有具有此名称的元素的列表:

var jobValue = document.getElementsByName('txtJob')[0].value  // first element in DOM  (index 0) with name="txtJob"

Or you assign an ID to the element:

或者为元素分配ID:

<input type="text" name="txtJob" id="txtJob" value="software engineer">

#3


9  

var word = document.getElementById("word").value;//by id
or
var word = document.forms[0].elements[0].value;//by index
//word = a word from form input
var kodlandi = escape(word);//apply url encoding

alert(escape(word));
or
alert(kodlandi);

the problem you are not using encoding for input values from form so not browser adds ones to ...

问题是您没有使用编码从表单输入值,所以浏览器没有添加到……

ontop has some problems as unicode encoding/decoding operations so use this function encoding strings/arrays

ontop有一些unicode编码/解码操作的问题,所以使用这个函数来编码字符串/数组

function urlencode( str ) 
{
// http://kevin.vanzonneveld.net3.    
// +   original by: Philip Peterson4.    
// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)5.    
// *     example 1: urlencode('Kevin van Zonneveld!');
// *     returns 1: 'Kevin+van+Zonneveld%21'7. 
   var ret = str; 
   ret = ret.toString();
   ret = encodeURIComponent(ret);
   ret = ret.replace(/%20/g, '+');
   return ret;
}


ex.
var word = "some word";
word = urlencode(word);

#4


5  

Set the id attribute of the input to txtJob. Your browser is acting quirky when you call getElementById.

将输入的id属性设置为txtJob。当你调用getElementById时,你的浏览器表现得很奇怪。

#5


5  

<!DOCTYPE html>
<html>
<body>
<label>Enter your Name here: </label><br>
<input type= text id="namehere" onchange="displayname()"><br>


<script>
function displayname() {
    document.getElementById("demo").innerHTML = 
document.getElementById("namehere").value;
}

</script>


<p id="demo"></p>

</body>
</html> 

#6


3  

If it is in a form then it would be:

如果它是一个形式,那么它就是:

<form name="jojo">
<input name="jobtitle">
</form>

Then you would say in javascript:

然后你会用javascript说:

var val= document.jojo.jobtitle.value

document.formname.elementname

#7


3  

Provided when you want the text box value. Simple one:

当您需要文本框值时提供。简单的一个:

<input type="text" value="software engineer" id="textbox">

var result = document.getElementById("textbox").value;

#8


3  

If you are using ASP.NET, the server-side code tags in these examples below will provide the exact control ID, even if you are using Master pages.

如果您正在使用ASP。NET,下面这些示例中的服务器端代码标记将提供精确的控制ID,即使您正在使用母版页。

Javascript:

Javascript:

document.getElementById("<%=MyControlName.ClientID%>").value;

JQuery:

JQuery:

$("#<%= MyControlName.ClientID %>").val();

#9


0  

If you are using any user control and want to get any text box values then you can use the below code:

如果您正在使用任何用户控件并希望获得任何文本框值,那么您可以使用以下代码:

var result = document.getElementById('LE_OtherCostsDetails_txtHomeOwnerInsurance').value;

Here, LE_OtherCostsDetails, is the name of the user control and txtHomeOwnerInsurance is the id of the text box.

在这里,LE_OtherCostsDetails是用户控件的名称,txtHomeOwnerInsurance是文本框的id。

#10


0  



The problem is that you made a Tiny mistake!

This is the JS code I use:

问题是你犯了一个小错误!这是我使用的JS代码:

var jobName = document.getElementById("txtJob").value;

You should not use name="". instead use id="".

您不应该使用name=""。而不是使用id = "。

#11


0  

You Need to change your code as below:-

您需要更改您的代码如下:-

<html>
<body>
<div>
<form align="center" method=post>
    <input id="mainText" type="text" align="center"placeholder="Search">

    <input type="submit" onclick="myFunction()" style="position: absolute; left: 450px"/>
</form>
</div>
<script>
function myFunction(){
    $a= document.getElementById("mainText").value;
    alert($a);
        }
</script>
</body>
</html>

#12


0  

because you used space between software engineer html/php will not support space between value under textbox, you have to use this code <input type="text" name="txtJob" value=software_engineer> It will work

因为您使用了software engineer html/php之间的空格,所以不支持文本框下的值之间的空格,所以您必须使用这段代码

#13


0  

 var jobValue=document.FormName.txtJob.value;

Try that code above.

试试以上代码。

jobValue : variable name.
FormName : Name of the form in html.
txtJob : Textbox name

jobValue:变量名。FormName: html中表单的名称。txtJob:文本框的名字

#14


0  

This should be simple using jquery:

使用jquery应该很简单:

HTML:

HTML:

  <input type="text" name="txtJob" value="software engineer">

JS:

JS:

  var jobValue = $('#txtJob').val(); //Get the text field value
  $('#txtJob').val(jobValue);        //Set the text field value

#15


0  

Set id for the textbox. ie,

设置文本框的id。也就是说,

<input type="text" name="txtJob" value="software engineer" id="txtJob"> 

In javascript

在javascript中

var jobValue = document.getElementById('txtJob').value

In Jquery

在Jquery

 var jobValue =  $("#txtJob").val();

#1


55  

+1 Gumbo: ‘id’ is the easiest way to access page elements. IE (pre version 8) will return things with a matching ‘name’ if it can't find anything with the given ID, but this is a bug.

+1 Gumbo:“id”是访问页面元素的最简单方法。IE (pre - version 8)将返回与给定ID匹配的“name”,但这是一个bug。

i am getting only "software".

我只得到“软件”。

id-vs-name won't affect this; I suspect what's happened is that (contrary to the example code) you've forgotten to quote your ‘value’ attribute:

id-vs-name不会影响;我怀疑发生了什么(与示例代码相反),您忘记引用您的“value”属性:

<input type="text" name="txtJob" value=software engineer>

#2


71  

Your element does not have an ID but just a name. So you could either use getElementsByName() method to get a list of all elements with this name:

元素没有ID,只有名称。因此,您可以使用getElementsByName()方法获取所有具有此名称的元素的列表:

var jobValue = document.getElementsByName('txtJob')[0].value  // first element in DOM  (index 0) with name="txtJob"

Or you assign an ID to the element:

或者为元素分配ID:

<input type="text" name="txtJob" id="txtJob" value="software engineer">

#3


9  

var word = document.getElementById("word").value;//by id
or
var word = document.forms[0].elements[0].value;//by index
//word = a word from form input
var kodlandi = escape(word);//apply url encoding

alert(escape(word));
or
alert(kodlandi);

the problem you are not using encoding for input values from form so not browser adds ones to ...

问题是您没有使用编码从表单输入值,所以浏览器没有添加到……

ontop has some problems as unicode encoding/decoding operations so use this function encoding strings/arrays

ontop有一些unicode编码/解码操作的问题,所以使用这个函数来编码字符串/数组

function urlencode( str ) 
{
// http://kevin.vanzonneveld.net3.    
// +   original by: Philip Peterson4.    
// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)5.    
// *     example 1: urlencode('Kevin van Zonneveld!');
// *     returns 1: 'Kevin+van+Zonneveld%21'7. 
   var ret = str; 
   ret = ret.toString();
   ret = encodeURIComponent(ret);
   ret = ret.replace(/%20/g, '+');
   return ret;
}


ex.
var word = "some word";
word = urlencode(word);

#4


5  

Set the id attribute of the input to txtJob. Your browser is acting quirky when you call getElementById.

将输入的id属性设置为txtJob。当你调用getElementById时,你的浏览器表现得很奇怪。

#5


5  

<!DOCTYPE html>
<html>
<body>
<label>Enter your Name here: </label><br>
<input type= text id="namehere" onchange="displayname()"><br>


<script>
function displayname() {
    document.getElementById("demo").innerHTML = 
document.getElementById("namehere").value;
}

</script>


<p id="demo"></p>

</body>
</html> 

#6


3  

If it is in a form then it would be:

如果它是一个形式,那么它就是:

<form name="jojo">
<input name="jobtitle">
</form>

Then you would say in javascript:

然后你会用javascript说:

var val= document.jojo.jobtitle.value

document.formname.elementname

#7


3  

Provided when you want the text box value. Simple one:

当您需要文本框值时提供。简单的一个:

<input type="text" value="software engineer" id="textbox">

var result = document.getElementById("textbox").value;

#8


3  

If you are using ASP.NET, the server-side code tags in these examples below will provide the exact control ID, even if you are using Master pages.

如果您正在使用ASP。NET,下面这些示例中的服务器端代码标记将提供精确的控制ID,即使您正在使用母版页。

Javascript:

Javascript:

document.getElementById("<%=MyControlName.ClientID%>").value;

JQuery:

JQuery:

$("#<%= MyControlName.ClientID %>").val();

#9


0  

If you are using any user control and want to get any text box values then you can use the below code:

如果您正在使用任何用户控件并希望获得任何文本框值,那么您可以使用以下代码:

var result = document.getElementById('LE_OtherCostsDetails_txtHomeOwnerInsurance').value;

Here, LE_OtherCostsDetails, is the name of the user control and txtHomeOwnerInsurance is the id of the text box.

在这里,LE_OtherCostsDetails是用户控件的名称,txtHomeOwnerInsurance是文本框的id。

#10


0  



The problem is that you made a Tiny mistake!

This is the JS code I use:

问题是你犯了一个小错误!这是我使用的JS代码:

var jobName = document.getElementById("txtJob").value;

You should not use name="". instead use id="".

您不应该使用name=""。而不是使用id = "。

#11


0  

You Need to change your code as below:-

您需要更改您的代码如下:-

<html>
<body>
<div>
<form align="center" method=post>
    <input id="mainText" type="text" align="center"placeholder="Search">

    <input type="submit" onclick="myFunction()" style="position: absolute; left: 450px"/>
</form>
</div>
<script>
function myFunction(){
    $a= document.getElementById("mainText").value;
    alert($a);
        }
</script>
</body>
</html>

#12


0  

because you used space between software engineer html/php will not support space between value under textbox, you have to use this code <input type="text" name="txtJob" value=software_engineer> It will work

因为您使用了software engineer html/php之间的空格,所以不支持文本框下的值之间的空格,所以您必须使用这段代码

#13


0  

 var jobValue=document.FormName.txtJob.value;

Try that code above.

试试以上代码。

jobValue : variable name.
FormName : Name of the form in html.
txtJob : Textbox name

jobValue:变量名。FormName: html中表单的名称。txtJob:文本框的名字

#14


0  

This should be simple using jquery:

使用jquery应该很简单:

HTML:

HTML:

  <input type="text" name="txtJob" value="software engineer">

JS:

JS:

  var jobValue = $('#txtJob').val(); //Get the text field value
  $('#txtJob').val(jobValue);        //Set the text field value

#15


0  

Set id for the textbox. ie,

设置文本框的id。也就是说,

<input type="text" name="txtJob" value="software engineer" id="txtJob"> 

In javascript

在javascript中

var jobValue = document.getElementById('txtJob').value

In Jquery

在Jquery

 var jobValue =  $("#txtJob").val();