[英]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.