Jquery 显示/隐藏元素基于 mysql 选择


Jquery show/hide element based on mysql selection

我想根据MySql值显示/隐藏元素

$(document).bind('DOMNodeInserted', '#composeHeaderTable0', function(event) {
    if ($('#data_parent_type0').val() == 'Leads') {
    $("#email_template0 option[value='151ddf5a-3fe8-84bd-50db-533545893c67']").hide();
    $("#email_template0 option[value='15a6040b-bbad-1a89-d62b-537de2b73832']").show();
}
    if ($('#data_parent_type0').val() == 'Contacts') {
    $("#email_template0 option[value='1b560788-07b2-0798-6b09-5335520bd9df']").hide();
    $("#email_template0 option[value='f15bf4b4-d174-f0d6-6f09-537c8d3e9693']").show();
}
    return false;
});

上面的脚本有效,但我需要根据Mysql调用显示隐藏:我有部分 php 对应的文件

<?php
    mysql_connect('mysql', 'admin', '123');
    mysql_select_db('mydb');
    $Leads0emailAddress0 = mysql_real_escape_string($_POST['Leads0emailAddress0']);
    $result = mysql_query('select id from email_templates where description = 'Customers' and deleted = 0;');
...............
?>

使用隐藏的表单元素,将其值设置为从 mysql 获得的结果。为该表单元素分配特定 ID。在jQuery中,使用ID引用该表单元素。这应该可以解决问题。

您的 mysql 脚本有错误

    $result = mysql_query('select id from email_templates where description = 'Customers' and deleted = 0;');

将其更改为

    $result = mysql_query(
              "SELECT id FROM 
                  email_templates 
                WHERE 
                  description = 'Customers' 
                AND deleted = 0"
             );

列出结果

$options = '';
while($row = mysql_fetch_assoc($result)) {
       $options .= '<option value="'.$row['id'].'">'.$row['id'].'</option>' . "'n";   
}
//display the options list in html where you want
<select id="email_template0">
   <?php echo $options; ?>
</select>

现在从 jQuery 句柄事件在下拉更改时

 $(function() {
 $("#email_template0").change(function() {
    alert( $('option:selected', this).val() );
    //do your hide and select here 
    });
 });