jQuery如何清除/替换变量中的存储值(.val()获得) [英] jQuery How to clear / replace the stored value in a variable (got from .val() )

查看:379
本文介绍了jQuery如何清除/替换变量中的存储值(.val()获得)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于有了以前的所有帮助,我设法做到了这一点:我能够在表中添加行并选择一个选项并显示文本输入. 这是小提琴链接.以前我使用多选,但现在更改为单选,因为多选在显示输出类型"时引起了问题(也许您可以提出更好的方法;))

Thanks to all the help previously, i have managed to come to this far: I am able to add row to the table and select an option and display a text input. Here is the Fiddle link. Previously i used multiselect but i changed to single select now because multiselect has caused problem in display my "Output Type" (may be you can suggest better ways to do it ;) )

现在,我面临另一个问题,其中存储在变量中的值将不会被新的输入值代替.因此,输出编号"列将仅显示第一个输入的数字.另一个奇怪的事情是,它只能从区域输入"中接受/更新输入,因为它被放置在html div中的第一位.我试图将其他人换到最顶端,同样的事情也发生了.

Now I am facing another problem where the value stored in the variable will not get replaced by the new input value. Therefore, "Output Number" column will only show the first number input. Another weird thing is, it can only take in/update input from 'Zone Input' as it is placed the first in the html div. i tried to change others to the top and same thing happened too.

var textInput = "";
$('.TextInput').empty();
textInput = $('.TextInput').val();

我尝试了.empty(),但是如您所见,它不起作用.每个div都有不同的ID.但是,如果我像以前那样仅使用其中一个测试了ID,请使用var textInput = $('#outputTextInput').val();,我该如何对其他人使用相同的ID?我是否使用OR ||之类的 var textInput = $('#outputTextInput').val() || $('#zoneTextInput').val();吗?

I tried .empty() but it doesn't work as you can see. Each of the div has different ID. But if i use ID like i tested for just one of them previously, var textInput = $('#outputTextInput').val(); , how can i do it the same for others? do i use OR || like var textInput = $('#outputTextInput').val() || $('#zoneTextInput').val(); ?

而且,因为它使用复选框而不是文本输入,我是否还必须为手臂"创建另一个功能吗?

Also, must i create another function for "Arm" since it is using a checkbox rather than text input?

更新:这是可行的,但不适用于我的程序:((< http://jsfiddle.net /yvonnezoe/MKfLU/1/

Update: This is working but not on my program :(( http://jsfiddle.net/yvonnezoe/MKfLU/1/

更新:我发现了这个问题!在这里查看我的问题:

UPDATE: I found the problem with this! See my problem here: jQuery vs jQuery Mobile - Does the script sequence matter?

推荐答案

尝试在您的代码中替换以下行:

Try to replace this lines in your code:

$('.TextInput').empty();
textInput =  $('.TextInput').val();

与此:

$('.TextInput').not($(this).prev('.TextInput')).val('');
textInput =  $(this).prev('.TextInput').val();

然后重试...

这是您的标记:

<input type="text" class="TextInput" id="zoneTextInput" value="">
<input type="submit" class="add" value="Add" />

现在,当您单击类别为add的添加"按钮时,$(this)代表当前作用域中的元素,而$(this).prev('.TextInput')代表其为类别TextInput之前的元素(此处为输入文本框).因此,$(this).prev('.TextInput').val()将为您提供带有单击的添加按钮的当前文本框的值.不管单击哪个添加按钮,$('.TextInput').val()都会为您提供第一个元素的值.

Now when you click on the Add button with class add, then $(this) represents the element in the current scope and the $(this).prev('.TextInput') represents the element previous to it with the class TextInput (Here, the input text-box). So, $(this).prev('.TextInput').val() will give you the value of the current textbox with add button you have clicked. Whereas, $('.TextInput').val() gives you the value of the first element no matter which add button is clicked.

有关更多详细信息: prev()

For more details: prev() and not()

这篇关于jQuery如何清除/替换变量中的存储值(.val()获得)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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