简体   繁体   English

将多个字段的值传递给jQuery

[英]Pass value of multiple fields to jquery

I want to pass multiple field on to jquery to do an check for example, but i only get value of the changed field passed to jquery how can i pass both field to jquery 我想将多个字段传递给jquery进行检查,例如,但是我只获取传递给jquery的更改字段的值,如何将两个字段都传递给jquery

Enter some text: 
<input type="text" name="txt1" value="Hello" onchange="myFunction(this.value)">
<input type="text" name="txt2" value="Hello" onchange="myFunction(this.value)">


<script>
function myFunction(txt1, txt2) {
  alert("The input value has changed. The new value is: " + txt1 + txt2);
}
</script>

The reason yours doesnt work is because of this this context, it refers to the item that has changed. 您不工作的原因是由于this上下文,它指的是已更改的项目。 You can access that from inside that input. 您可以从该输入内部访问它。

You can however access them in another way, eg getElementsByName : 但是,您可以通过其他方式访问它们,例如getElementsByName

function myFunction() {
    let text1 = document.getElementsByName('txt1')[0].value;
    let text2 = document.getElementsByName('txt2')[0].value;
    alert("The input value has changed. The new value is: " + txt1 + txt2);
}

<input type="text" name="txt1" value="Hello" onchange="myFunction()">
<input type="text" name="txt2" value="Hello" onchange="myFunction()">

I've now selected it via name. 我现在通过名称选择了它。 If you only intend to use it via javascript, I recommend dropping the name attribute and use id instead. 如果您仅打算通过javascript使用它,建议您删除name属性并改用id You can then replace getElementsByName('txt1')[0] with getElementsById('txt1') , which is a little cleaner to read and is (slightly) faster. 然后,您可以将getElementsByName('txt1')[0]替换为getElementsById('txt1') ,这样阅读起来会更干净一些,并且(略)快一些。


If you want to step it up a notch: It's not standard practice to add eventhandlers right into the inputs, but to hook them on: 如果您想提高一点:将事件处理程序直接添加到输入中,而是将它们挂接到钩子上,这不是标准做法。

<input id="txt1" value="Hello" /> <!-- type=text is the default type, you can ommit it -->
document.getElementById('text1').onchange = myFunction;

Ok, the reason why your code didn't work, you are trying to pass in a value of an undefined element. 好的,由于您的代码无法正常工作的原因,您正在尝试传入未定义元素的值。 Remember that you are firing onchange event, and once you change one of the input fields the other remains the same not firing the onchange event. 请记住,您正在触发onchange事件,并且一旦更改了其中一个输入字段,其他输入字段将保持不变而不触发onchange事件。 so to make sure you get the value of another input field only if one of the field is changed. 因此,请确保仅在更改一个输入字段的值的情况下,才能获取另一个输入字段的值。

Use document.querySelectorAll('classOfTheFields'); 使用document.querySelectorAll('classOfTheFields');

and loop through using for and get the values as done below. 并遍历使用for和获取值,如下所示。

 <input type="text" name="txt1" class="inner" value="Hello" onchange="myFunction(this.value)"> <input type="text" name="txt2" class="inner" value="Hello" onchange="myFunction(this.value)"> <script> var allValue = []; function myFunction() { text = document.querySelectorAll('.inner'); for (var I = 0;I<text.length;I++){ allValue = text[I].value; console.log(text[I].value); } } </script> 

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM