简体   繁体   English

javascript根据条件的选择选项值将值添加到id

[英]javascript add value to id based on select option value conditional

First off, I know I should know this. 首先,我知道我应该知道这一点。 I am just not connecting the dots. 我只是没有连接点。

I have jquery working with a select menu so that when an option is selected, it's value is added to a hidden id in my form. 我让jquery使用选择菜单,以便在选择一个选项时将其值添加到表单中的隐藏ID中。

What I need, is that when an option is selected in this same select menu, depending on which option is selected, an email becomes the value in a different hidden id in the form. 我需要的是,当在同一选择菜单中选择一个选项时,根据选择的选项,电子邮件将成为表单中其他隐藏ID的值。

I have an if conditional in my jsfiddle but it is not working. 我的jsfiddle中有一个if条件,但它不起作用。 Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢! Peter T 彼得·T

        jQuery(document).ready(function() {
      // pass the title of the selected topic option to a hidden 'topic' form field
      jQuery('select#recipient_email').change(function() {
          var topic = jQuery('select#recipient_email option:selected').attr('title')
            // set the hidden input's value
            jQuery('#college2').val(topic);
            //var topic_var = jQuery('#college2').val(topic);
            if ((jQuery('#college2').val(topic)) == "Var 1")
            {
                $("#recipient_email_user").val("John.Doe@mail.com");
            }
            if ((jQuery('#college2').val(topic)) == "Var 2")
            {
                $("#recipient_email_user").val("Jane.Doe@mail.com");
            }
      })
    }) 

jsfiddle 的jsfiddle

This line is wrong 这行是错的

if ((jQuery('#college2').val(topic)) == "Var 1")

You are setting the value and it returns the jQuery object, it is the equivilant of jQuery('#college2') == "Var 1" 您正在设置值,它返回jQuery对象,它与jQuery('#college2') == "Var 1"

If should just be 如果应该只是

if (topic == "Var 1")

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

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