jquery - Get value of selected option in select field in a Wordpress form

So I am trying to get which option is selected without submitting it to server side. I have 2 drop down fields, the firs

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

What 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
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745295134a4621092.html

相关推荐

  • jquery - Get value of selected option in select field in a Wordpress form

    So I am trying to get which option is selected without submitting it to server side. I have 2 drop down fields, the firs

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信