79

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>

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();

but it doesn't seem to be working

4

8 回答 8

152

你必须使用value attribute才能获得它的价值

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

尝试 -

var text = $('#DynamicValueAssignedHere').find('input[name="FirstName"]').val();
于 2012-07-25T16:36:31.967 回答
56

它可以比你正在做的简单得多。

HTML:

<input id="myField" type="text" name="email"/>

JavaScript:

// getting the value
var email = $("#myField").val();

// setting the value
$("#myField").val( "new value here" );
于 2012-07-25T16:36:04.357 回答
10

另一种方法,无需搜索字段 html:

var $form = $('#' + DynamicValueAssignedHere).find('form');
var formData = $form.serializeArray();
var myFieldName = 'FirstName';
var myFieldFilter = function (field) {
  return field.name == myFieldName;
}
var value = formData.filter(myFieldFilter)[0].value;
于 2015-11-16T14:42:09.557 回答
3

$("form").submit(function(event) {
    
      var firstfield_value  = event.currentTarget[0].value;
     
      var secondfield_value = event.currentTarget[1].value; 
     
      alert(firstfield_value);
      alert(secondfield_value);
      event.preventDefault(); 
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" >
<input type="text" name="field1" value="value1">
<input type="text" name="field2" value="value2">
</form>

于 2016-08-12T11:45:09.813 回答
1

如果你知道输入的 id,你只需要使用这个:

var value = $("#inputID").val();
于 2012-07-25T16:38:50.950 回答
1
var textValue = $("input[type=text]").val()

这将获得所有文本框的所有值。您可以使用 children、firstchild 等方法进行磨练。例如通过 form $('form[name=form1] input[type=text]') 更容易使用 ID 来定位元素,但如果它是纯动态的,您可以获得所有输入值然后使用 JS 循环。

于 2012-07-25T16:40:03.777 回答
0

您可以通过以下方式获取任何输入字段值 $('input[fieldAttribute=value]').val()

这是一个例子

displayValue = () => {

  // you can get the value by name attribute like this
  
  console.log('value of firstname : ' + $('input[name=firstName]').val());
  
  // if there is the id as lastname
  
  console.log('value of lastname by id : ' + $('#lastName').val());
  
  // get value of carType from placeholder  
  console.log('value of carType from placeholder ' + $('input[placeholder=carType]').val());

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="formdiv">
    <form name="inpForm">
        <input type="text" name="firstName" placeholder='first name'/>
        <input type="text" name="lastName" id='lastName' placeholder='last name'/>
        
        <input type="text" placeholder="carType" />
        <input type="button" value="display value" onclick='displayValue()'/>
        
    </form>
</div>

于 2019-01-31T09:14:19.270 回答
0

您可以尝试以下几行:

$("#DynamicValueAssignedHere .formdiv form").contents().find("input[name='FirstName']").prevObject[1].value
于 2019-01-25T07:22:43.680 回答