Jquery获取表单字段值 [英] Jquery get form field value

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

问题描述

我正在使用jquery模板在同一页面上动态生成多个元素。每个元素都是这样的

I am using a jquery template to dynamically generate multiple elements on the same page. Each element looks like this

<div id ="DynamicValueAssignedHere">
    <div class="something">Hello world</div>
    <div class="formdiv">
        <form name="inpForm">
            <input type="text" name="FirstName" />
            <input type="submit" value="Submit" />
        </form>
    </div>
</div>

我想使用Jquery处理提交表单。如果出现问题,我还想将表单值恢复为之前的值。我的问题是
如何使用Jquery获取输入框的值?例如,通过执行

I would like to use Jquery to process the form on submit. I would also like to revert the form values to their previous values if something should go wrong. My question is How can I get the value of input box using Jquery? For example, I can get the value of the div with class "something" by doing

var something = $(#DynamicValueAssignedHere).children(".something").html();

以类似的方式,我希望能够检索文本框的值。现在,我试过了

In a similar fashion, I want to be able to retrieve the value of the textbox. Right now, I tried

var text = $(#DynamicValueAssignedHere).children(".formdiv").findnext('input[name="FirstName"]').val();

但它似乎无法正常工作

推荐答案

您必须使用值属性才能获得其值

You have to use value attribute to get its value

<input type="text" name="FirstName" value="First Name" />

试试 -

var text = $('#DynamicValueAssignedHere').find('input[name="FirstName"]').val();

这篇关于Jquery获取表单字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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