2015-03-26 6 views
1

Как я могу получить значение моего поля поиска при нажатии на сообщение?Получить конкретное значение ввода

<form method="post" action="index.php?page=search" id="FrontSearchForm"> 
    <input type="text" name="searchstring" id="searchstring" placeholder="Search" /> 
</form> 

SearchString должна быть извлечена в этом JQuery файле:

(function($) { 
 
function getUrlVars() 
 
{ 
 
    var vars = [], hash; 
 
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
 
    for(var i = 0; i < hashes.length; i++) 
 
    { 
 
     hash = hashes[i].split('='); 
 
     vars.push(hash[0]); 
 
     vars[hash[0]] = hash[1]; 
 
    } 
 
    return vars; 
 
} 
 
var searchstring = $('#searchstring').val(); 
 
var mipage = getUrlVars()["id"]; 
 

 
$AjaxPHP = "SearchAjax.php"; 
 

 
\t $.fn.scrollPagination = function(options) { 
 
\t \t 
 
\t \t var settings = { 
 
\t \t \t nop  : 16, // The number of posts per scroll to be loaded 
 
\t \t \t offset : 0, // Initial offset, begins at 0 in this case 
 
\t \t \t error : 'Ikke mere indhold!', // When the user reaches the end this is the message that is 
 
\t \t \t        // displayed. You can change this if you want. 
 
\t \t \t delay : 500, // When you scroll down the posts will load after a delayed amount of time. 
 
\t \t \t    // This is mainly for usability concerns. You can alter this as you see fit 
 
\t \t \t scroll : true // The main bit, if set to false posts will not load as the user scrolls. 
 
\t \t \t    // but will still load if the user clicks. 
 
\t \t } 
 
\t \t 
 
\t \t // Extend the options so they work with the plugin 
 
\t \t if(options) { 
 
\t \t \t $.extend(settings, options); 
 
\t \t } 
 
\t \t 
 
\t \t // For each so that we keep chainability. 
 
\t \t return this.each(function() { \t \t 
 
\t \t \t 
 
\t \t \t // Some variables 
 
\t \t \t $this = $(this); 
 
\t \t \t $settings = settings; 
 
\t \t \t var offset = $settings.offset; 
 
\t \t \t var busy = false; // Checks if the scroll action is happening 
 
\t \t \t     // so we don't run it multiple times 
 
\t \t \t 
 
\t \t \t // Custom messages based on settings 
 
\t \t \t if($settings.scroll == true) $initmessage = '<div class="ScrollLoader"><p>Scroll for at se mere indhold</p></div>'; 
 
\t \t \t else $initmessage = '<div class="ScrollLoader"><p>Klik for at se mere indhold</p></div>'; 
 
\t \t \t 
 
\t \t \t // Append custom messages and extra UI 
 
\t \t \t $this.append('<div class="content"></div><div class="loading-bar">'+$initmessage+'</div>'); 
 

 
\t \t \t function getData() { 
 
\t \t \t \t 
 
\t \t \t \t // Post data to ajax.php 
 
\t \t \t \t $.post($AjaxPHP, { 
 
\t \t \t \t \t action  : 'scrollpagination', 
 
\t \t \t \t  number  : $settings.nop, 
 
\t \t \t \t  offset  : offset, 
 
\t \t \t \t \t mipage \t \t : mipage, 
 
\t \t \t \t \t searchstring : searchstring, 
 
\t \t \t \t \t  
 
\t \t \t \t }, function(data) { 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t // Change loading bar content (it may have been altered) 
 
\t \t \t \t \t $this.find('.loading-bar').html($initmessage); 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t // If there is no data returned, there are no more posts to be shown. Show error 
 
\t \t \t \t \t if(data == "") { 
 
\t \t \t \t \t \t $this.find('.loading-bar').html($settings.error); \t 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else { 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t \t // Offset increases 
 
\t \t \t \t \t  offset = offset+$settings.nop; 
 
\t \t \t \t \t \t  
 
\t \t \t \t \t \t // Append the data to the content div 
 
\t \t \t \t \t  \t $this.find('.content').append(data); 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t \t // No longer busy! \t 
 
\t \t \t \t \t \t busy = false; 
 
\t \t \t \t \t } \t 
 
\t \t \t \t \t \t 
 
\t \t \t \t }); 
 
\t \t \t \t \t 
 
\t \t \t } \t 
 
\t \t \t 
 
\t \t \t getData(); // Run function initially 
 
\t \t \t 
 
\t \t \t // If scrolling is enabled 
 
\t \t \t if($settings.scroll == true) { 
 
\t \t \t \t // .. and the user is scrolling 
 
\t \t \t \t $(window).scroll(function() { 
 
\t \t \t \t \t 
 
\t \t \t \t \t // Check the user is at the bottom of the element 
 
\t \t \t \t \t if($(window).scrollTop() + $(window).height() > $this.height() && !busy) { 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t \t // Now we are working, so busy is true 
 
\t \t \t \t \t \t busy = true; 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t \t // Tell the user we're loading posts 
 
\t \t \t \t \t \t $this.find('.loading-bar').html('<div class="ScrollLoader"><p><img src="ajax-loader.gif" /> Henter indhold...</p></div>'); 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t \t // Run the function to fetch the data inside a delay 
 
\t \t \t \t \t \t // This is useful if you have content in a footer you 
 
\t \t \t \t \t \t // want the user to see. 
 
\t \t \t \t \t \t setTimeout(function() { 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t getData(); 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t }, $settings.delay); 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t } \t 
 
\t \t \t \t }); 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t // Also content can be loaded by clicking the loading bar/ 
 
\t \t \t $this.find('.loading-bar').click(function() { 
 
\t \t \t 
 
\t \t \t \t if(busy == false) { 
 
\t \t \t \t \t busy = true; 
 
\t \t \t \t \t getData(); 
 
\t \t \t \t } 
 
\t \t \t 
 
\t \t \t }); 
 
\t \t \t 
 
\t \t }); 
 
\t } 
 
\t 
 
})(jQuery);

Ну .. Как вы можете видеть, этот файл Jquery общается с SearchAjax.php, где я получение результатов из базы данных.

Проблемы я думаю, var searchstring = $('#searchstring').val();

+0

оповещение значение SearchString или 'console.log (SearchString)' и проверить консоль для его стоимости, а также проверить, что размещен в Ajax запроса в консоли –

+0

значения надстройки = «» во входной 'тревоги (searchstring); 'и посмотреть, что предупреждает пчела – Ghostman

+0

Попробуйте использовать двойные кавычки: var searchstring = $ (" # searchstring "). val() ; – liquidflo

ответ

0

Если вы отправите форму на сервер, значение входного будет доступно в вашем index.php кода. Если вы не включите это значение в свой вывод, то это значение не будет доступно для вашей стороны. Javascript

. Один из способов сделать это состоит в том, чтобы иметь search.php, содержащий скрытый ввод, который имеет переданное ему значение searchvalue index.php

<input type="hidden" id="searchstring" value="valueFromIndex.php" /> 
Смежные вопросы