在Javascript中获取输入值 [英] Getting input value in Javascript

查看:72
本文介绍了在Javascript中获取输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在javascript中获取输入的值,但它不起作用。以下是我的测试页面的代码

I am trying to get the value of the input in a javascript, but its not working. Given below is the code of my test page

<%@ Page Title="" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"

CodeFile="test3.aspx.vb" Inherits="test3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
<script language="javascript">

    function Validation() {

        var TestVar = document.getElementById('txtName').value;
        alert(TestVar);

    }

</script>
<table>
<tr>
<td align="left"><input type="text"  runat="server" id="txtName" /></td>
</tr>

<tr>
    <td align="left" class="style3">
        <asp:Button runat="server" ID="btnsubmit" Text="Submit" 

        Height="26px" OnClientClick="Validation()"  />
       
        </td></tr>
       
        </table>
</asp:Content>

推荐答案

首先,为什么你把你的表格html元素放入你的HeadContent容器?你不应该只是将脚本元素放在HeadContent中,同时将表放在MainContent容器中吗?



要修复你的问题你需要修改javascript行所以它编写控件ClientID。



更改此行...

First off, why are you putting your table html element into your HeadContent container? Shouldn't you only be putting the script element in the HeadContent while putting the table in the MainContent container?

To fix your issue you need to modify the javascript line so it writes the controls ClientID.

Change this line...
var TestVar = document.getElementById('txtName').value;



对此...


To this...

var TestVar = document.getElementById('<%= txtName.ClientID %>').value;



如果控件被标记为runat =Server那么你不能在javascript中引用控件的id,因为它在.aspx页面中,因为asp.net将根据控件的父控件预先挂起名称。例如,当'txtName'id的id被呈现到响应流时,它实际上变成类似'MainContent_txtName'的id。要解决此问题,请执行以上操作,在javascript中引用ClientID。


If the control is tagged as runat="Server" then you can't reference the control's id in javascript as it is in the .aspx page because asp.net will pre-pend to the name based on the control's parent controls. For example, your id of 'txtName' id actually becomes something like 'MainContent_txtName' when it gets rendered to the response stream. To address this you do the above where you reference the ClientID in your javascript.


我相信您需要从输入中删除runat =server。这是没有必要的,因为你希望客户端运行该功能。

I believe you need to remove the runat="server" from your input. That is not necessary since you want the client to run that function.
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="test3.aspx.vb" Inherits="test3" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
<script language="javascript">
 
    function Validation() {
 
        var TestVar = document.getElementById('txtName').value;
        alert(TestVar);
 
    }
 
</script>
<table>
<tr>
<td align="left"><input type="text" id="txtName" /></td>
</tr>
 
<tr>
    <td align="left" class="style3">
        <asp:Button runat="server" ID="btnsubmit" Text="Submit" 
        Height="26px" OnClientClick="Validation()"  />
       
        </td></tr>
       
        </table>
</asp:Content>


hi


解决方案2 会正常工作..如果没有尝试这样,



在浏览器中加载页面并右键单击 - > ;查看源,在页面上找到 txtName 控件。并记下 id 吧..



使用 id 在您的 javascript 代码..
hi
solution 2 will work fine.. if not try like this,

load the page in browser and right click -> view source , find the txtName control on the page. and note down the id for it..

use that id in your javascript code..


这篇关于在Javascript中获取输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆