[英]javascript add value to id based on select option value conditional
首先,我知道我应该知道这一点。 我只是没有连接点。
我让jquery使用选择菜单,以便在选择一个选项时将其值添加到表单中的隐藏ID中。
我需要的是,当在同一选择菜单中选择一个选项时,根据选择的选项,电子邮件将成为表单中其他隐藏ID的值。
我的jsfiddle中有一个if条件,但它不起作用。 任何帮助表示赞赏。 谢谢! 彼得·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");
}
})
})
这行是错的
if ((jQuery('#college2').val(topic)) == "Var 1")
您正在设置值,它返回jQuery对象,它与jQuery('#college2') == "Var 1"
如果应该只是
if (topic == "Var 1")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.