簡體   English   中英

根據已檢查的單選輸入更改表單操作和文本輸入名稱

[英]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,並且我正嘗試參加Big League。

JS中的行:

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

無法正常工作。 <?php ?>標記已執行的服務器端。 JS代碼在瀏覽器中執行。

您需要明確指定表單的操作網址:類似於:

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();

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM