0

我想做的是同时输出文本框值和下拉列表选择值。原因是我想最终通过 mysql db 表对结果进行排序。现在......当我更改文本框时,它将添加到下拉列表值中,这就是我想要的。但是当我更改下拉列表值时,它不会识别文本框值并恢复到文档加载时的值。当我在文本框上“键入”或“更改”下拉列表值时,如何获取这两个值并回显它们并能够更改它们并查看更改。提前致谢。

jQuery

$(document).ready(function () {
    var type_term = $("#type option:selected").text()
    var search_term = $("#search").val()
    $.post('search_db.php',{type_term:type_term, search_term:search_term}, function (data) {
        $('#search_results').html(data) 
    });
    $('#search').keyup(function() {
        var search_term = $("#search").val()
        $.post('search_db.php',{type_term:type_term, search_term:search_term}, function (data) {
            $('#search_results').html(data) 
        });
    });
    $('#type').change(function() {
        var type_term = $("#type option:selected").text()
        $.post('search_db.php',{type_term:type_term, search_term:search_term}, function (data) {
           $('#search_results').html(data)      
        });
    });
});

HTML

<input id="search" type="text" value="Value">

<select id="type">
<option>Type1</option>
<option>Type2</option>
</select>

<div id="search_results"></div>

搜索数据库.php

$type_term = $_POST['type_term'];
$search_term = $_POST['search_term'];

echo $type_term;
echo $search_term;
4

1 回答 1

0

尝试为每个事件的两个表单元素设置变量,而不仅仅是您正在更改的那个。

$(document).ready(function () {
var type_term = $("#type option:selected").text()
var search_term = $("#search").val()
$.post('search_db.php',{type_term:type_term, search_term:search_term}, function (data) {
    $('#search_results').html(data) 
});
$('#search').keyup(function() {
    var search_term = $("#search").val()
    var type_term = $("#type option:selected").text()
    $.post('search_db.php',{type_term:type_term, search_term:search_term}, function (data) {
        $('#search_results').html(data) 
    });
});
$('#type').change(function() {
    var search_term = $("#search").val()
    var type_term = $("#type option:selected").text()
    $.post('search_db.php',{type_term:type_term, search_term:search_term}, function (data) {
       $('#search_results').html(data)      
    });
});
});
于 2013-05-06T02:08:31.740 回答