使用下拉列表添加/删除搜索框

Using dropdown to add/remove search boxes

本文关键字:删除 搜索 添加 下拉列表      更新时间:2023-09-26

我有一个下拉列表,其中有3个类别。我希望这样,当用户选择一个类别时,会显示该类别的正确搜索框数。然后,我希望输入到搜索框中的文本保存为URL中的变量。这是我得到的

http://jsfiddle.net/2yWzc/1/

HTML:

<form class="list" action="table.php" method="get">
    <table>
        <tbody>
            <tr class="name">
                <td>First Name:</td>
                <td><input type="text" class="searchBox" name="q1" /></td>
            </tr>
            <tr class="name">
                <td>Last Name:</td>
                <td><input type="text" class="searchBox" name="q2" /></td>
            </tr>
            <tr class="owner">
                <td>Owner:</td>
                <td><input type="text" class="searchBox" name="q1" /></td>
            </tr>
            <tr class="dlp">
                <td>Text 1:</td>
                <td><input type="text" class="searchBox" name="q1" /></td>
            </tr>
            <tr class="dlp">
                <td>Text 2:</td>
                <td><input type="text" class="searchBox" name="q2" /></td>
            </tr>
            <tr class="dlp">
                <td>Text 3:</td>
                <td><input type="text" class="searchBox" name="q3" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="SEARCH" /></td>
            </tr>
        </tbody>
    </table>
    <br>
    <select id="options">
        <option value="name">Option 1</option>
        <option value="owner">Option 2</option>
        <option value="dlp">Option 3</option>
    </select>
</form>

JS:

$('#options').change(function() {
    if ($(this).val() == 'name') {
        $('.name').show();
        $('.owner').hide();
        $('.dlp').hide();
    } else if ($(this).val() == 'owner') {
        $('.name').hide();
        $('.owner').show();
        $('.dlp').hide();
    } else if ($(this).val() == 'dlp') {
        $('.name').hide();
        $('.owner').hide();
        $('.dlp').show();
    }
});
$(function(){
    $('form').bind('change', function () {
        var url = $(this).val();
        if (url) {
            window.location = url;
        }
        return false;
    });
});

这显示了正确数量的搜索框,但不会将搜索框中的文本保存在变量中。这似乎也不是一个好方法(如果你知道正确的方法,就给我指明正确的方向。这是我唯一能做的有效的方法)。在此之前,我每个类别有一个搜索框,所以我的JS代码是这个

(function($) {
    $(function(){
        $('form').bind('change', function () {
            var url = $(this).val(); // get selected value
            if (url) { // require a URL
                window.location = url; // redirect
            }
            return false;
        });
    });
})(jQuery)​;

然而,我不知道如何让它在多个搜索框中工作。我只希望显示的搜索框中的变量通过URL传递(同样,也许这不是正确的方式?)。

有人能帮我吗?感谢

您可以尝试以下操作。

事件处理程序绑定到窗体的submit事件。在它中,我们获得了表单中存在的具有type="text"的所有输入元素的列表,并只拾取使用filter方法可见的字段。然后我们遍历元素列表并形成dataString。此dataString的格式为q=a&q1=b,并将附加到URL中。encodeURIComponent用于对dataString进行编码。

$(function(){
    var dataString = "";
    var url="sample.htm";
    var count=0;
    $('form').bind('submit', function () {
        $('form input[type="text"]').filter(':visible').each(function(){
            if(count === 0)
                dataString += this.name + "=" + $(this).val();
            else
                dataString += '&' + this.name + "=" + $(this).val();
            count++;
        });
        //console.log(dataString);
        dataString += "&t="+$("#options").val(); //appending the value of the select box
        if (url) {
            window.location = url + "?" + encodeURIComponent(dataString); //added for the URL encode
        }
        return false;
    });
});

更新的工作演示

这不是最好的方法,但这里有一个对您的jsfiddle的更新,它将执行您想要的

$(function(){
    $('input[type=submit]').bind('click', function () {
        count = 0;
        url = '';
        $("input[type=text]").each(function(){
            if($(this).is(":visible")){
                count++;
                if(count != 1){
                    url += "&";
                }
                url += "var"+count+"="+$(this).val();
            }
        });
        if(count != 0){
            url += "&";
        }
        url += "count="+count;
        url += "&option="+$("#options").val();
        alert(url);
        return false;
    });
});

http://jsfiddle.net/2yWzc/4/