[英]JQuery Getting Value of Input Box in On Click Event
There's a lot of other threads on this topic but none of them solve my issue. 关于这个主题还有很多其他话题,但是没有一个能解决我的问题。 I'm trying to get the value from a input field which I just inserted.
我试图从我刚刚插入的输入字段中获取值。 I insert my input box using this line of javascript:
我使用以下这行javascript插入输入框:
processName.innerHTML = '<input type="text" id="ProcessNameChange" value="' + oldName + '" >';`
Then I try to access it using: 然后,我尝试使用以下方法访问它:
var newName = $(this).closest("#ProcessNameChange").val();
Both of these are wrapped in an event, a button click event. 两者都包装在一个事件中,即按钮单击事件。 Below is the entire function if you'd like all of it.
如果需要的话,下面是整个功能。 Also, if you have a better way of coding anything I wrote, feel free to share it.
另外,如果您有更好的方式编码我编写的任何内容,请随时分享。
$('.Edit').click(function () {
if ($(this).attr("id") != "submitUpdate") {
$(this).attr("id", "submitUpdate");
$(this).text("Submit");
var closestTR = $(this).closest("tr");
var processName = closestTR.children()[2];
var processDescription = closestTR.children()[3];
var processID = closestTR.children()[4];
var oldName = processName.innerHTML;
var oldDesc = processDescription.innerHTML;
processName.innerHTML = '<input type="text" id="ProcessNameChange" value="' + oldName + '" >';
processDescription.innerHTML = '<input type="text" id="ProcessDescChange" value="' + oldDesc + '" > ';
submitButtonClick();
} else {
var newName = $(this).closest("#ProcessNameChange").val();
var newDesc = $(this).closest("#ProcessDescChange").val();
var processID = $(this).closest("tr").children()[4];
}
--> Related to Comments: The HTML for the input box always stays as shown below: It's all made via a repeater than modified with javascript. ->与注释相关:输入框的HTML始终保持如下所示:都是通过中继器完成的,而不是使用javascript修改的。
<tr>
<td class="grid-main-detail"></td>
<td>
<input id="ProcessNameChange" type="text" value="PNameOld">
</td>
<td>
<input id="ProcessDescChange" type="text" value="PDescOld">
</td>
<td>
<td>3547556300824952452</td>
<td>Me </td>
<td>7/19/2013 2:32:48 PM</td>
</tr>
Thanks! 谢谢!
You are using the closest selector incorrectly 您错误地使用了最接近的选择器
see this fiddle 看到这个小提琴
http://jsfiddle.net/Td6Wx/3/ http://jsfiddle.net/Td6Wx/3/
<div class="test"></div>
<a href="#" class="test2">asdasdas</a>
closest() ( http://api.jquery.com/closest ) travels up through its ANCESTORS, in your case .Edit is not a child of the input so of course it will not work. 在您的情况下,最近的()( http://api.jquery.com/closest )遍历它的祖先。编辑不是输入的子元素,因此它当然不起作用。 If you post your html structure we can probably provide a correct solution
如果您发布html结构,我们可能会提供正确的解决方案
One question: why are you accessing the value of the input like 一个问题:您为什么要像这样访问输入的值
var newName = $(this).closest("#ProcessNameChange").val();
instead of: 代替:
var newName = $("#ProcessNameChange").val();
? ?
Since the item is unique (by virtue of its id
attribute), I do not see a need to use .closest()
to get to it (and its value). 由于该项是唯一的(凭借其
id
属性),因此我看不到需要使用.closest()
来获取.closest()
及其值)。
Do you really need to select the input field in such a way? 您是否真的需要以这种方式选择输入字段?
I would recommend to get rid of innerHTML
to stay focused on jQuery methods such as .append()
and $()
to create elements. 我建议您摆脱
innerHTML
以专注于.append()
和$()
类的jQuery方法来创建元素。 Besides I suppose you are looking for td
tr's children. 另外我想您正在寻找
td
tr的孩子。
var oldName = processName.text();
var oldDesc = processDescription.text();
processName.append($('<input type="text" id="ProcessNameChange" value="' + oldName + '" >'));
processDescription.append($('<input type="text" id="ProcessDescChange" value="' + oldDesc + '" > '));
Hope this helps, 希望这可以帮助,
R. R.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.