1

I'm a long-time lurker, first-time poster on Stack Overflow (which has saved me in quite a few programming projects before). I have a unique problem that I haven't been able to find by searching the site, so I'll describe it here.

I am creating an online order form where users can select a food category. That category is sent to another page of code that runs a query on the database and returns all items within that category to a drop-down box named ItemXSelect (where X is the item number). I have not had trouble with this. However, I am having trouble using the value that the user chooses for ItemXSelect in another AJAX Query. Specifically, the AJAX request does not send at all (according to Firebug). I'm hoping it's something simple like a syntax error, but I've been at it for days.

The code below is Jquery/AJAX written inside of a PHP Echo statement and has certain characters escaped. $itemNum determines the value of the X in ItemXSelect.

\$(\"#Item" . $itemNum . "Select option:selected\").change(function() {
var sel = \$(\"#Item" . $itemNum . "Select option:selected\").text());
    \$.ajax({
        type: \"POST\",
        url: \"sizearray.php\",
        data: { category: \"Cold_Sandwiches\" , selection: sel },
        dataType: \"text\"
    }).done(function(result) {
        \$(\"#Item" . $itemNum . "Size\").html(result);
    }); 
    \$(\"#Item" . $itemNum . "Size\").show();
});

This has been troubling me for days. My thanks go to anyone who can help me find what's wrong here (or even at least try to).

=====================================================================================

EDIT 1

Thanks to your help, I've cleaned up the code by wrapping all the JQuery in a PHP EOD statement instead. That way, I don't have to escape the whole thing. Below is the updated code. The suggestions below have not worked so far to answer the main question, but at least the syntax is much neater.

jQuery("#Item{$itemNum}Select option:selected").change(function() {
    var sel = jQuery("#Item{$itemNum}Select option:selected").text());
        jQuery.ajax({
            type: "POST",
            url: "sizearray.php",
            data: { category: "Cold_Sandwiches" , selection: sel },
            dataType: "text"
        }).done(function(result) {
            jQuery("#Item{$itemNum}Size").html(result);
        }); 
        jQuery("#Item{$itemNum}Size").show();
    });
4

2 回答 2

0

或者您可以在客户端使用 jQuery 进行实时绑定。

编辑

在你的 JavaScript

    $('select').change(function(){
       alert(this.options[this.selectedIndex].value);
    });

HTML

<select>
   <option value="apple">Apple</option>
   <option value="orange">Orange</option>
</select>

因此,对于您添加的每个选择(通过 AJAX 动态)将自动绑定到该更改事件。

或者,您可以采用更复杂但更不容易出错的方法,您只需以 JSON 格式从服务器请求数据并使用数据填充控件。

于 2013-10-29T18:21:05.937 回答
0

所有这些台词看起来都很有趣。我不知道 PHP,但在我看来,您的引号不匹配。

\$(\"#Item" . $itemNum . "Select option:selected\").change(function() {

\$(\"#Item" . $itemNum . "Size\").html(result);

\$(\"#Item" . $itemNum . "Size\").show();
于 2013-10-29T18:12:11.040 回答