[英]How to get updated form input value via jQuery/Javascript?
I see lots of discussion about setting and getting a field value via jQuery. 我看到了很多有关通过jQuery设置和获取字段值的讨论。 But my issue is that I am unable to get a value after I submit, then edit the value in an input field, then submit again. 但是我的问题是,提交后无法获取值,然后在输入字段中编辑值,然后再次提交。 So the first time it works, the second time it doesn't. 因此,它第一次起作用,第二次却没有起作用。
Here's my scenario: I have a form that is used to manage filters for a dataset: 这是我的情况:我有一个用于管理数据集过滤器的表单:
<form id="filtersForm">
<div class="row">
<div class="col-md-12">
<div id="addContactFilter">
<div class="row filterRow" id="contact_filter_780b902d">
<div class="col-md-4">
<!-- Selectize dropdown of fields to query by -->
<div class="control-group">
<select id="filterField_780b902d" class="selectizeField selectized" name="filterField_780b902d" placeholder="Select field..." tabindex="-1" style="display: none;">
<option value="firstName" selected="selected">First Name</option>
</select>
<div class="selectize-control selectizeField single">
<div class="selectize-input items has-options full has-items">
<div data-value="firstName" class="item">First Name</div>
<input type="text" autocomplete="off" tabindex="" style="width: 4px; opacity: 0; position: absolute; left: -10000px;">
</div>
<div class="selectize-dropdown single selectizeField" style="display: none; visibility: visible; width: 248px; top: 34px; left: 0px;">
<div class="selectize-dropdown-content">
<div data-value="firstName" data-selectable="" class="option selected">First Name</div>
<div data-value="lastName" data-selectable="" class="option">Last Name</div>
<div data-value="organization" data-selectable="" class="option">Organization</div>
<div data-value="jobTitle" data-selectable="" class="option">Job Title</div>
<div data-value="city" data-selectable="" class="option">City</div>
<div data-value="state" data-selectable="" class="option">State</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<select id="filterOperator_780b902d" class="form-control">
<option value="=">equals</option>
<option value="contains">contains</option>
</select>
</div>
<div class="col-md-4">
<div id="contact_filter_value_780b902d">
<!-- THIS IS THE FIELD IN QUESTION -->
<input class="form-control" type="text" name="filterValue_780b902d" id="filterValue_780b902d" data-type="String" placeholder="Input Value">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row filterCriteria">
<div class="col-md-12">
<input class="btn btn-default" type="submit" value="Submit">
</div>
</div>
</form>
When I first submit the form, I am able to successfully get the value for the input field with id #filterValue_780b902d
. 首次提交表单时,我能够成功获取ID为#filterValue_780b902d
的输入字段的值。 But if the user edits the value in the form, then submits again, the original value is returned. 但是,如果用户编辑表单中的值,然后再次提交,则返回原始值。
Here's how I try to retrieve the value (I am using Meteor JS, which provides an event map on the click event with an event
parameter): 这是我尝试检索值的方式(我正在使用Meteor JS,它使用event
参数提供有关click事件的事件映射 ):
var target = event.target,
fieldName = 'filterValue_' + fieldId;
thisVal1 = target[fieldName].value,
thisVal2 = template.find( "[name='" + fieldName + "']" ).value,
thisVal3 = $( '#' + fieldName ).attr('value'),
thisVal4 = $( '#' + fieldName ).val();
When I submit the form the first time with the input value "Meghan", here's the response logs: 当我第一次使用输入值“ Meghan”提交表单时,以下是响应日志:
contactFilters.filtersForm submit
contactFilters.filtersForm.fieldId: 780b902d
contactFilters.filtersForm.fieldName: filterValue_780b902d
contactFilters.filtersForm.thisVal1: Meghan
contactFilters.filtersForm.thisVal2: Meghan
contactFilters.filtersForm.thisVal3: undefined
contactFilters.filtersForm.thisVal4: Meghan
But when I change the input value to "Karen", here's what I get: 但是,当我将输入值更改为“ Karen”时,得到的是:
contactFilters.filtersForm submit
contactFilters.filtersForm.fieldId: 780b902d
contactFilters.filtersForm.fieldName: filterValue_780b902d
contactFilters.filtersForm.thisVal1: Meghan
contactFilters.filtersForm.thisVal2: undefined
contactFilters.filtersForm.thisVal3: undefined
contactFilters.filtersForm.thisVal4: undefined
So three questions: 所以三个问题:
thisVal1
not update when the text of the field is updated? 为什么在字段文本更新时thisVal1
不更新? thisVal2
and thisVal4
become undefined
? 为什么thisVal2
和thisVal4
变得undefined
? Thanks in advance for your help! 在此先感谢您的帮助!
i believe the correct way to get value in jQuery is to use the val() function. 我相信在jQuery中获得价值的正确方法是使用val()函数。
so you should probably be using: 因此,您可能应该使用:
var target = event.target,
fieldName = 'filterValue_' + fieldId;
myValue = $( '#' + fieldName ).val();
as stated here: https://stackoverflow.com/a/15903284/6368401 and here: http://api.jquery.com/val/ 如此处所述: https : //stackoverflow.com/a/15903284/6368401和此处: http : //api.jquery.com/val/
in javascript 在JavaScript中
document.getElementById("input_id").value
other javascript methods explained here: How do I get the value of text input field using JavaScript? 此处介绍的其他JavaScript方法: 如何使用JavaScript获取文本输入字段的值?
to answer your 'undefined' questions: 回答您的“未定义”问题:
var target = event.target,
fieldName = 'filterValue_' + fieldId;
thisVal1 = target[fieldName].value,
this is getting the original value from the postback. 这是从回发中获取原始值。
thisVal2 = template.find( "[name='" + fieldName + "']" ).value,
i cant answer this one, as i dont know the answer 我无法回答这个,因为我不知道答案
thisVal3 = $( '#' + fieldName ).attr('value');
the attribute "value" was never defined in the markup. 标记中从未定义属性“值”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.