【问题标题】:jQuery dialog not displaying div on changejQuery 对话框在更改时不显示 div
【发布时间】:2011-07-18 15:42:59
【问题描述】:

我正在使用对话框显示表单并在提交时回显消息。效果很好。我还有一个下拉菜单,用于从填充第二个下拉列表中选择值。这本身就可以很好地工作,但是如果我尝试将两者合并,则不会显示 div id="divId" 或从更改中填充。现在我将是第一个承认我的 jQuery 知识至少可以说是基础的人,所以我将感谢专家的任何帮助。我已包含代码以供您注意并等待您的 cmets。谢谢

这段代码在单独的function.js中

// Function to add box

function addbox() {

    $("#boxaddform").dialog({
        autoOpen: false,
        resizable: true,
        modal: true,
        title: 'Submit a box intake request',
        width: 470,
        beforeclose: function (event, ui) {
            $("#addbox").html("");

        }

    });

    $('#boxsubmit').click(function () {

        var box = $('.box').val();
        var service = $('#service').val();
        var authorised = $('.authorised').val();
        var data = 'box=' + box + '&authorised=' + authorised + '&service=' + service;
        $.ajax({
            type: "POST",
            url: "boxesadd.php",
            data: data,
            success: function (data) {
                $("#boxform").get(0).reset();
                $('#addbox').html(data);
                //$("#form").dialog('close');
                $("#flex1").flexReload();

            }
        });
        return false;

    });

    $("#boxaddform").dialog('open');

}

html

<script language="javascript" type="text/javascript">
      $(function() {
            $("#company").change(function() {
              if ($(this).val()!="") $.get("getOptions.php?customer=" + $(this).val(), function(data) {
                $("#divId").html(data);
                });
              });
      });
</script>

<!--- Form to add box -->

<div id="boxaddform" style="display:none;">
  <form id="boxform" method="post" class="webform" name="boxform" />

        <label for="company">Select a Company:</label>
                <select name="company" id="company" />
                    <option SELECTED VALUE="">Select a Company</option>
                        <?php
                                do {  
                                ?>
                    <option value="<?php echo $row_Recordsetcust['customer']?>"><?php echo $row_Recordsetcust['customer']?></option>
                        <?php

                    } 
                                while ($row_Recordsetcust = mysql_fetch_assoc($Recordsetcust));
                                $rows = mysql_num_rows($Recordsetcust);
                if($rows > 0)

                    {
                                mysql_data_seek($Recordsetcust, 0);
                                $row_Recordsetcust = mysql_fetch_assoc($Recordsetcust);
                    }

                ?>
            </select><br />
            <div id="divId"></div><br />
        <label for="service">Enter service level:</label>
                <select name="service" id="service">
                    <option SELECTED VALUE="">Select an option</option>
                    <option value="Standard">Standard</option>
                    <option value="Rapid">Rapid</option>
                </select>
                <br />
        <label for="box">Enter a Box#:</label>
                <input id="box" name="box" type="text" class="text ui-widget-content ui-corner-all inputbox box" />

        <label for="authorised">Requested By:</label>
                <input name="authorised" type="text" class="text ui-widget-content ui-corner-all inputbox authorised" id="authorised" value="<?php echo $_SESSION['kt_name_usr']; ?>" /><br />
    <div id="addbox"></div>
      <button id="boxsubmit" class="submit">Submit</button>
  </form>
</div>

getOptions.php

<?php
    $customer = mysql_real_escape_string( $_GET["customer"] ); // not used here, it's the customer choosen

    $con = mysql_connect("localhost","root","");
    $db = "sample";
      if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }

        mysql_select_db($db, $con);
        $query_rs_select_address2 = sprintf("SELECT * FROM company_com where idcode_com = '$customer'");
        $rs_select_address2 = mysql_query($query_rs_select_address2, $con) or die(mysql_error());
        $row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2);
        $totalRows_rs_select_address2 = mysql_num_rows($rs_select_address2);



          echo 'Select delivery address'.'<select name="customeraddress">';
          echo '<option value="">Select delivery address</option>';
          while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2))
          {
          $address=$row_rs_select_address2['address1_com']. " ".
          $row_rs_select_address2['address2_com']. " ".
          $row_rs_select_address2['address3_com']. " ".
          $row_rs_select_address2['town_com']. " ".
          $row_rs_select_address2['postcode_com'];
          echo '<option
          value="address">'.$address.'</option>';
          }
          echo '</select>';
?>

【问题讨论】:

  • 脚本好长....
  • @mihai 如何压缩它?它拥有我需要的一切。我见过更大的:-)
  • 你能发布渲染的 HTML 吗?我认为我们不需要看看它是如何创建的......
  • @onekidney 我无法发布呈现的 html,因为对话框输出不会呈现(至少不在我的浏览器中)输出,但会显示在 firebug 中。如果我错了,请纠正我,但是无论代码有多大,查看代码是否有益,例如,在我的代码中可能有一个很容易被发现的错误?谢谢
  • :) 不幸的是,您错了,因为我们不想阅读您的整个程序。有一个姐妹网站可以请求code reviews。这个网站只是为了回答特定的问题,很少需要回答那么多代码。

标签: php javascript jquery dialog


【解决方案1】:

试试这个

$("#company").live('change', function() {
  if ($(this).val()!="") $.get("getOptions.php?customer=" + $(this).val(), function(data)  {
    $("#divId").html(data);
  });
});

在您提供的脚本的第一部分。

我的想法是,更改处理代码可能没有捕获下拉列表并且根本没有运行。 为了测试那个 put alert('test');在你当前的 $("#company").change(function(){...});块。

【讨论】:

  • 做到了。活着和改变有什么区别。我读过那个触发器是用来改变的?。谢谢
  • 哎呀,很快就联系上了。它在 IE8 中引发错误。解析错误:语法错误,第 18 行 C:\wamp\www\sample\admin\getOptions.php 中的意外 '='
  • 第 18 行围绕该区域开始:echo 'Select Delivery address'.'
  • 您可以在 google 中搜索“jquery live”并阅读相关内容。基本上,使用“实时”,您可以将事件附加到所有 DOM 元素,包括创建页面时不存在的元素。这包括所有动态添加/创建的元素,例如您的下拉菜单。
  • 这是一个 PHP 错误,对吧?您很可能在每个浏览器中都有它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 2011-02-10
  • 1970-01-01
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多