简体   繁体   English

如何将值从文本框存储到asp.net vb.net应用程序中的javascript变量中

[英]How to store value from a textbox into a javascript variable in a asp.net vb.net application

I am working with asp.net; 我正在使用asp.net; vb.net; vb.net; java script. Java脚本。 and want to store some values in the java script variable. 并希望将一些值存储在java脚本变量中。

<%@ Page Title="" Language="VB" MasterPageFile="~/_resx/E4.master" AutoEventWireup="true" CodeFile="new.aspx.vb" Inherits="E4_Jobs_new" ValidateRequest="false" %>

<%@ Register Src="~/_controls/ucApplicationQuestions.ascx" TagPrefix="Application"
TagName="Questions" %>

  <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
     <script type="text/javascript">
         var id = '<%= ModeID%>',
             mode = '<%= Mode%>',
             employer = '<%= Employer.Name %>',
             jobtitle = document.getElementById(<%= txtTitle.ClientID%>);

     </script>

     <asp:RequiredFieldValidator runat="server" ControlToValidate="txtTitle" Display="None" ErrorMessage="xx" ValidationGroup="NewJob" EnableViewState="False" />
         <div class="form-element">
              <input type="text" id="txtTitle" runat="server" maxlength="64" /></div>
</asp:Content>

Now as you can see in my code i have four java script variables. 现在,如您在我的代码中看到的,我有四个Java脚本变量。

id, mode, employer  & jobtitle

for the first 3 variable i am getting the correct result. 对于前三个变量,我得到了正确的结果。 but for the last one i am not being able to get the correct result. 但是对于最后一个,我无法获得正确的结果。

what i want is the input value in the text box will be in the fourth variable (before submitting the form). 我想要的是文本框中的输入值将在第四个变量中(在提交表单之前)。

i am trying to get the value by using document.getelementbyid and store it like 我试图通过使用document.getelementbyid获取值并将其存储为

jobtitle = document.getElementById(<%= txtTitle.ClientID%>)

i have also tried 我也尝试过

jobtitle = document.getElementById(<%= txtTitle.ClientID%>).value

but its not working. 但它不起作用。 (and also the editor doesnot suggest me to use value. it suggests valueof) (而且编辑也不建议我使用value。它建议valueof)

when i see the source code i see the following result 当我看到源代码时,我看到以下结果

<script type="text/javascript">
    var id = '609',
        mode = 'draft',
    employer = 'MyPeopleBiz',
    jobtitle = document.getElementById(ctl00_MainContent_txtTitle);

</script>

and the source code for the textfield is like this 文本字段的源代码是这样的

<input name="ctl00$MainContent$txtTitle" type="text" id="ctl00_MainContent_txtTitle" maxlength="64" />

how do i get the value and store the result in the javascript variable. 我如何获取值并将结果存储在javascript变量中。

尝试这个

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

If this code is running server side, hence before the DOM is generated then you will not be able to access any DOM nodes as they will not yet exist. 如果此代码在服务器端运行,因此在生成DOM之前,您将无法访问任何DOM节点,因为它们尚不存在。

Instead you could put the <%= txtTitle.ClientID%> into a hidden field on the page, and then use jQuery's $(document).ready() functionality to obtain the result of the hidden field and use it as required (ie assigning it to your global variable jobtitle . 相反,您可以将<%= txtTitle.ClientID%>放到页面上的隐藏字段中,然后使用jQuery的$(document).ready()功能获取隐藏字段的结果并根据需要使用它(即分配将其转换为全局变量jobtitle

Try adding a [0] before the .value: 尝试在.value之前添加[0]:

var jobtitle = document.getElementById("<%= txtTitle.ClientID%>")[0].value; var jobtitle = document.getElementById(“ <%= txtTitle.ClientID%>”)[0] .value;

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

相关问题 如何从asp.net获取JavaScript中应用程序变量的值? - How to get value of application variable in JavaScript from asp.net? 如何获取ASP.net变量的值到JavaScript文本框中 - How to get the value of an ASP.net variable into a javascript textbox 将变量从javascript传递到代码VB.Net之后的ASP.NET - pass variable from javascript to ASP.NET behind code VB.Net 如何从javascript的asp.net文本框中检索值? - How to retrieve value from asp.net textbox from javascript? 如何从javascript警报中获取价值到asp.net中的文本框 - How to get value from javascript alert to textbox in asp.net 如何从asp.net到javascript查找文本框值 - how to find textbox value from asp.net to javascript 如何将值从asp.net数据列表传递到javascript文本框? - How to pass a value from asp.net datalist to javascript textbox? 将数据集传递到数组(VB.Net,ASP.NET,JavaScript)之后,从数据行返回多列以用于文本框控件 - Return multiple columns from datarow for a textbox control after passing the dataset to an array (VB.Net, ASP.NET, JavaScript) 如何将数据库中的数据从asp.net/vb.net放入javascript数组 - How to put data from a database into javascript array from asp.net/vb.net 是否可以使用Javascript破坏ASP.NET(VB.NET)设置的会话变量? - Is it possible to destroy a session variable set by ASP.NET (VB.NET) with Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM