根据选中的无线电输入更改表单操作和文本输入名称


Change Form Action and Text Input Name based on Radio Input Checked

我可以通过以下代码获得一些帮助吗?

$(document).ready(function() {
    $('#searchform').submit(function() {
        var action = '';
        if($('.action_url').val() == 'l_catalog') {
            action = 'http://catalog.site.com/uhtbin/cgisirsi.exe/x/0/0/57/5';
            $("#s").attr('name','searchdata1');
        } else if ($('.action_url').val() == 'l_wordpress'){
            action = '<?php get_bloginfo('url') ?>';
            $("#s").attr('name','s');
        }
        $(this).attr('action', action);
        return true; // submit
    });
    $('.action_url').change();
});

我正在尝试使用此函数将表单的操作设置为图书馆目录搜索页面或wordpress搜索页面。目前,单选按钮将选择/取消选择,并且将仅搜索目录,而不是wordpress。我最初在网上找到此代码用于选择表单,但现在必须使用单选按钮。(有一些困难(。为此,PHP 是:

$search_form .= '<form id="searchform" name="searchform" method="get" action="' . get_bloginfo('url') .'/">';
$search_form .= "'n" . "'t" . "'t";
$search_form .= '<div>';
$search_form .= "'n" . "'t" . "'t". "'t";
$search_form .= '<input type="radio" name="search_where" id="catalog" class="action_url" value="l_catalog" /> <label for="catalog">Library Catalog</label> <input type="radio" name="search_where" id="wordpress" class="action_url" value="l_wordpress" /> <label for="wordpress">Library Website</label>'; // <option value='"l_catalog'">Library Catalog</option> 'n 't 't 't 't <option value='"l_wordpress'">Library Website</option>";
$search_form .= "'n" . "'t" . "'t". "'t";
if (is_search()) {
        $search_form .= '<input id="s" name="searchdata1" type="text" value="' . esc_html(stripslashes($_GET['s'])) .'" size="' . $search_form_length . '" tabindex="1" />';
} else {
        $value = __('To search, type and hit enter', 'thematic');
        $value = apply_filters('search_field_value',$value);
        $search_form .= '<input id="s" name="searchdata1" type="text" value="' . $value . '" onfocus="if (this.value == ''' . $value . ''') {this.value = '''';}" onblur="if (this.value == '''') {this.value = ''' . $value . ''';}" size="'. $search_form_length .'" tabindex="1" />';
}
$search_form .= "'n" . "'t" . "'t". "'t";
$search_form .= '<input name="srchfield1" type="hidden" value="GENERAL^SUBJECT^GENERAL^^words or phrase">';
$search_form .= '<input name="sort_by" type="hidden" value="-PBYR">';
$search_form .= '<input name="user_id" type="hidden" value="WSC">';
$search_form .= '<input name="password" type="hidden" value="">';
$search_form .= "'n" . "'t" . "'t". "'t";
$search_submit = '<input id="searchsubmit" name="searchsubmit" type="submit" value="' . __('Search', 'thematic') . '" tabindex="2" />';
$search_form .= apply_filters('thematic_search_submit', $search_submit);
$search_form .= "'n" . "'t" . "'t";
$search_form .= '</div>';
$search_form .= "'n" . "'t";
$search_form .= '</form>';

敢肯定这很混乱,因为我不太了解PHP或Javascript,而且我正在尝试在大联盟中打球。

JS中的行:

action = '<?php get_bloginfo('url') ?>';

行不通。 <?php ?>标记已执行的服务器端。JS代码在浏览器中执行。

您需要指定表单的操作 URL 显式:类似于:

action = 'http://catalog.site.com/uhtbin/cgisirsi.exe/x/0/0/57/5';

我建议您创建一个隐藏字段:

$search_form .= '<input id="blog_url" type="hidden" value="' . get_bloginfo('url'). '">';

然后,您的 JS 可以引用此浏览器端:

action = $('#blog_url').val();

祝你好运!