admin管理员组文章数量:1418660
So I am trying to get which option is selected without submitting it to server side. I have 2 drop down fields, the first one is of last names and second one is of first names. Both are populated from a Google Sheet. E.g. If user selects the Last name "Doe" then the second dropdown automatically searches for all names ending with last name "Doe" and populates second dropdown with the corresponding first names.
I am using Ninja Forms and trying to get the selected option using jQuery. Below is my code which I am adding in the functions.php file:
<script language="JavaScript" type="text/javascript">;
// First Method:
var value = jQuery(function($){$("#field-id option:selected").val()});
console.log(value);
//Second Method:
var value = jQuery(function($){$("#field-id :selected").children("option").filter(":selected").text()});
console.log(value);
//Third Method:
var value = jQuery(function($){$("#field-id :selected").text()});
console.log(value);
</script>
Unfortunately all three methods give the output [object Object] when I try to document.write the values. I try console.log to log the values but I get a very long console outputs for each log and none of it contains the selected value in them.
So I am trying to get which option is selected without submitting it to server side. I have 2 drop down fields, the first one is of last names and second one is of first names. Both are populated from a Google Sheet. E.g. If user selects the Last name "Doe" then the second dropdown automatically searches for all names ending with last name "Doe" and populates second dropdown with the corresponding first names.
I am using Ninja Forms and trying to get the selected option using jQuery. Below is my code which I am adding in the functions.php file:
<script language="JavaScript" type="text/javascript">;
// First Method:
var value = jQuery(function($){$("#field-id option:selected").val()});
console.log(value);
//Second Method:
var value = jQuery(function($){$("#field-id :selected").children("option").filter(":selected").text()});
console.log(value);
//Third Method:
var value = jQuery(function($){$("#field-id :selected").text()});
console.log(value);
</script>
Unfortunately all three methods give the output [object Object] when I try to document.write the values. I try console.log to log the values but I get a very long console outputs for each log and none of it contains the selected value in them.
Share Improve this question asked Jul 25, 2019 at 19:36 Shritama SenguptaShritama Sengupta 11 silver badge2 bronze badges1 Answer
Reset to default 1What are you trying to accomplish? You are assigning the value at the wrong place. It should be:
jQuery(function($){
var value = $("#field-id option:selected").val();
console.log(value);
//And then here use the value
});
本文标签: jqueryGet value of selected option in select field in a Wordpress form
版权声明:本文标题:jquery - Get value of selected option in select field in a Wordpress form 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745295133a2652028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论