获取特定输入值

时间:2015-03-26 12:33:17

标签: javascript php jquery ajax

当我按帖子时如何检索我的搜索字段的值?

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

应该在这个jQuery文件中获取searchstring:

&#13;
&#13;
(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";

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

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

嗯..正如你所看到的,这个jQuery文件与SearchAjax.php通信,我从数据库中获取结果。

我认为问题是var searchstring = $('#searchstring').val();

1 个答案:

答案 0 :(得分:0)

如果您将表单提交回服务器,则输入的值将在index.php代码中提供。如果您未在输出中包含此值,那么您的客户端将无法使用该值javascript

执行此操作的一种方法是让search.php包含一个隐藏的输入,该输入具有从index.php传递给它的搜索值

<input type="hidden" id="searchstring" value="valueFromIndex.php" />