如何使用 JQuery 检索文本框值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1320088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:13:21  来源:igfitidea点击:

How do I retrieve a textbox value using JQuery?

jquery

提问by leora

Possible Duplicate:
How do I get the value of a textbox using jQuery?

可能的重复:
如何使用 jQuery 获取文本框的值?

on form submit, i am trying to grab the value of the textbox below and shove it in an url

在表单提交时,我试图获取下面文本框的值并将其推入一个 url

<input type="text" style="width: 300px" id="fromAddress" name="from" value="" />&nbsp;

here is my jquery code:

这是我的 jquery 代码:

<script type='text/javascript'>
     $(document).ready(function() {

         $(":submit").click(function(e) {

             var from = $("input#fromAddress").text;
             $('div#printdirec').html('<a target="_blank" href="http://maps.google.com/addr=' + from + '&daddr">Print Directions</a>');

         });

     });
</script>

when i look at the URL, it doesn't have the correct from address.

当我查看 URL 时,它没有正确的发件人地址。

回答by Darko Z

You need to use the val()function to get the textbox value. textdoes not exist as a property only as a function and even then its not the correct function to use in this situation.

您需要使用该val()函数来获取文本框值。text不作为属性仅作为函数存在,即使这样它也不是在这种情况下使用的正确函数。

var from = $("input#fromAddress").val()

val()is the standard function for getting the value of an input.

val()是获取输入值的标准函数。

回答by Yoosaf Abdulla

Just Additional Info which took me long time to find.what if you were using the field name and not id for identifying the form field. You do it like this:

只是我花了很长时间才找到的附加信息。如果您使用字段名称而不是 id 来识别表单字段,该怎么办。你这样做:

For radio button:

对于单选按钮:

var inp= $('input:radio[name=PatientPreviouslyReceivedDrug]:checked').val();

For textbox:

对于文本框:

var txt=$('input:text[name=DrugDurationLength]').val();