[英]Get textbox value in jQuery
No matter what I've tried, I cannot get the value of a textbox in my jQuery function.无论我尝试过什么,我都无法在我的 jQuery 函数中获取文本框的值。 I know there is a value there, I can see it.
我知道那里有一个价值,我可以看到它。 However my code below comes back at "undefined" for the value.
但是,我下面的代码返回值的“未定义”。 I cannot figure out why.
我不明白为什么。
I have a js file that is referenced on my asp.net page.我有一个在我的 asp.net 页面上引用的 js 文件。 On my asp.net page, I have a button that triggers a function on click.
在我的 asp.net 页面上,我有一个按钮可以在点击时触发一个功能。
<asp:ImageButton ID="BtnNext" runat="server" OnClientClick="TransactionType(); return false;" ImageUrl="~/next.png" />
My jQuery code:我的jQuery代码:
function TransactionType() {
var PType = $("#TxtPTypeValue").val;
alert(PType);
if (PType == "CC") {
SubmitCCTransaction();
} else if (PType == "EC") {
SubmitECheckTransaction();
};
};
The part that isn't working is this: var PType = $("#TxtPTypeValue").val;
不工作的部分是这样的:
var PType = $("#TxtPTypeValue").val;
which comes back as "undefined".返回为“未定义”。
You can fix in this way你可以用这种方式修复
$("#TxtPTypeValue").val();
$("#BtnNext").on("click", function(){ console.log($("#TxtPTypeValue").val()); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type = "text" id = "TxtPTypeValue" /> <button id="BtnNext">BtnNext</button>
Updated更新
According to @sh1rts's comment, you should also make sure that根据@sh1rts 的评论,您还应该确保
Add a ClientIdMode="Static" to that definition, without this it won't have that ID on the actual page
将 ClientIdMode="Static" 添加到该定义中,没有它,实际页面上将没有该 ID
Correct Syntax正确的语法
$('#TxtPTypeValue').val()
When used to return value:用于返回值时:
This method returns the value of the value attribute of the FIRST matched element.此方法返回第一个匹配元素的 value 属性的值。
When used to set value:用于设置值时:
This method sets the value of the value attribute for ALL matched elements.此方法为所有匹配的元素设置 value 属性的值。
Val
is a function, so you have to call it like this val()
. Val
是一个函数,所以你必须像这样调用它val()
。
Try the below code.试试下面的代码。
function TransactionType() { var PType = $("#TxtPTypeValue").val(); alert(PType); if (PType == "CC") { // SubmitCCTransaction(); } else if (PType == "EC") { // SubmitECheckTransaction(); }; };
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea id="TxtPTypeValue"></textarea> <button onclick="TransactionType()">Print Value</button>
$(document).ready(function(){ $('input:text').val(); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="txtvalue" value="425656">
如果要使用服务器端控件的值,则必须传递控件的客户端 ID,例如:
$("#<%=TxtPTypeValue.ClientId%>").val();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.