如何在html select的onChange上传递参数
我是 JavaScript 和 jQuery 的新手。我想显示一个组合框-A,它是一个 HTML
如何传递完整的组合框及其选择 id
,以及如何传递 onChange 事件的其他参数?
I am a novice at JavaScript and jQuery. I want to show one combobox-A, which is an HTML <select>
with its selected id
and contents at the other place on onChange().
How can I pass the complete combobox with its select id
, and how can I pass other parameters on fire of the onChange event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
上面的示例获取 OnChange 上组合框的选定值事件。
The above example gets you the selected value of combo box on OnChange event.
在某些情况下,另一种方法可能很方便,即将所选
的值直接传递给函数,如下所示:
Another approach wich can be handy in some situations, is passing the value of the selected
<option />
directly to the function like this:关于如何在 jQuery 中执行此操作:
您还应该知道 Javascript 和 jQuery 并不相同。 jQuery 是有效的 JavaScript 代码,但并非所有 JavaScript 都是 jQuery。您应该查找差异并确保您使用的是合适的。
For how to do it in jQuery:
You should also know that Javascript and jQuery are not identical. jQuery is valid JavaScript code, but not all JavaScript is jQuery. You should look up the differences and make sure you are using the appropriate one.
JavaScript 解决方案
或
或
JavaScript Solution
or
or
我发现@Piyush 的答案很有帮助,只是补充一下,如果您以编程方式创建一个选择,那么有一种重要的方法可以实现这种可能并不明显的行为。假设您有一个函数并且创建了一个新的选择:
正常的行为可能是说
但这实际上并不允许您指定传入的参数,因此您可以这样做:
并且您可以设置参数。如果您采用第一种方式,那么您将获得
onchange
函数的参数将取决于浏览器。第二种方式似乎跨浏览器工作得很好。I found @Piyush's answer helpful, and just to add to it, if you programatically create a select, then there is an important way to get this behavior that may not be obvious. Let's say you have a function and you create a new select:
The normal behavior may be to say
But this does not really allow you to specify that argument passed in, so instead you may do this:
And you are able to set the parameter. If you do it the first way, then the argument you'll get to your
onchange
function will be browser dependent. The second way seems to work cross-browser just fine.jQuery 解决方案
如何获取所选选项的文本值
选择元素通常具有您想要访问的两个值。
首先是要发送到服务器的值,这很简单:
第二个是选择的文本值。
例如,使用以下选择框:
如果您想在选择第一个选项(而不仅仅是“1”)时获取字符串“Mr”,您可以按以下方式执行此操作:
另请参阅
jQuery solution
How do I get the text value of a selected option
Select elements typically have two values that you want to access.
First there's the value to be sent to the server, which is easy:
The second is the text value of the select.
For example, using the following select box:
If you wanted to get the string "Mr" if the first option was selected (instead of just "1") you would do that in the following way:
See also
这对我有帮助。
对于选择:
对于单选/复选框:
This is helped for me.
For select:
For radio/checkbox:
你可以尝试下面的代码
You can try bellow code
html模板:
Js代码:
Html template:
Js code:
您只能使用“this.value”发送值
You can send only value with 'this.value'
以防万一有人正在寻找 React 解决方案而无需下载附加依赖项,您可以编写:
确保在构造函数中绑定 Changed() 函数,如下所示:
Just in case someone is looking for a React solution without having to download addition dependancies you could write:
Make sure to bind the changed() function in the constructor like:
一旦我编写了这段代码,只是为了解释 select 的 onChange 事件,您可以将此代码保存为 html 并查看其工作原理的输出。并且很容易理解。
this code once i write for just explain onChange event of select you can save this code as html and see output it works.and easy to understand for you.
这对我有用 onchange =
setLocation($(this).val())
这里。
This worked for me onchange =
setLocation($(this).val())
Here.
简单地:
输出:
Klaus 或 Andrew 取决于 selectedIndex 是什么。如果您需要该值,只需将 .text 替换为值即可。但是,如果它只是您想要的值(而不是选项中的文本),则使用 document.getElementById('SMS_recipient').value
Simply:
Output:
Klaus or Andrew depending on what the selectedIndex is. If you are after the value just replace .text with value. However if it is just the value you are after (not the text in the option) then use document.getElementById('SMS_recipient').value
我认为现在附加事件处理程序的首选方法(而不是 onchange= 属性)是
I would think the preferred way to attach the event handler now (instead of onchange= attribute) is