English 中文(简体)
显示根据选定框中选择的文字
原标题:Display text depending on a choice made in select box

我怎么能运行一个 PHP 查询, 只有在选择一个选项时才能运行, 选择一个从选中的下调中选择的选项, 但每次选择不同的选项时都会刷新这些选项?

例如,这里是 HTML :

<html>
<head>
<script type="text/javascript" src="/hr/includes/jui/js/jquery-ui-1.8.20.min.js"</script>
<script>
$(document).ready(function() {
    $( #select_box_1 ).change(function() {
       if($(this).val() !=   )
        $.ajax({
            type:  get ,
            url:  平衡 1.php ,
            data:  value  + $(this).val() ,
            success: function(data) {
                $("#result-div").html(data);
            }
        });
     }
    });
});
</script>
</head>

<body>

<form name="form" id="form" method="post" action="validate.php">

<div id="select_box_1">
<select name="drop_down">
<option value="">Please choose</option>
<option value="1">John</option>
<option value="2">Bob</option>
<option value="3">Mike</option>
</select>
</div>

<div id="result-div">

</div>

<input type="submit">

</form>

</body>

</html>

平衡 1.php

<?php 
print "GET Value:".$_GET[ value ];
?>

我想根据所选择的值在选择框下运行一个查询 - 例如,当没有选择任何值时, 没有运行查询。 如果用户从列表中选择 John, 那么查询会运行 ID=1 的位置, 并显示约翰的结果 。 如果用户随后选择 Bob, 查询应该再次运行 ID=2 的位置?

最佳回答

这正在起作用:

[...]
<select name="drop_down" id="sel_something">
[...]

$( #sel_something ).change(function() {
    if($(this).val() !=   ) {
        $.ajax({
            type:  get ,
            url:  someurl ,
            data: {  value  : $(this).val() } ,
            success: function(data) {
                // do your stuff
            }
        });
    }
});

我给所选数据线提供了ID, 并修改了数据线( 并添加了一些语法校正 ) 。

问题回答

我建议你使用jquery.ajax, 这比你想象的队友更强大! 下面是让它发挥作用的示例代码。

$( #select_box_1 ).change(function() {
   if($(this).val() !=   )
    $.ajax({
        type:  get ,
        url:  URL To Your PHP Script ,
        data:  value  + $(this).val() ,
        success: function(data) {
            $("#result-div").html(data);
        }
    });
 }
});

< 加强 > EDIT : $_GET[value] 将保持您php脚本中的值, 您可以使用该值查询数据库, 并将结果或输出返回到 ajax 调用中, 然后您就可以简单地在 html 视图中填充数据

如果您每次重新装入页面, 数值就是下调时的变化, 那么您可以在 SELECT 标签中使用更改属性 。

当值被更改时重新装入页面并在 URL 中设置 GEG 变量。 如果 GEG 变量为 NULL, 那么不相应处理 PHP 代码... 其它进程 。

尝试一下这样的东西:

$("#select_box_1").change(function(){

    if ($(this).val() != "") {

      $.ajax({
         type: "POST",
         url: "foo.php",
         data: "id=" + $("#select_box_1").val(),
         success: function(msg){
           $("#result").text(msg);
         }
      });

    }

});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签