有人可以帮我在Jquery中写这个/ if

时间:2014-08-15 02:32:09

标签: php jquery

   <!-- Search input -->
   <form method="post" action="query.php" id="searchform" name="search" class="search">
    <input id="inpBusiness" type="text" placeholder="Find a Business by Name" name="Business"/>
    <input id="inpLocation" type="text" placeholder="Or by a Massachusetts City/Town" name="City"/>
    <input id="btnSearch" type="submit" value="Search VetsDirectory" name="btnSearch"/><br/>
   </form>
   <hr />

   </td>
  </tr>
  <tr>
   <td>
    <!-- This is where the featured listings and search Query will load -->
    <h3 align="left">FEATURED BUSINESS LISTINGS:</h3>
    <div id="inputhere">
    <?PHP
    include 'php.php';
    ?>
    </div>

这是我的表单,提交后,我需要检查用户是否使用了inpBusiness或inpLocation进行搜索。如果他们使用location进行搜索,我需要将action设置为query.php,如果他们使用business to search我需要动作为query2.php,如果两者都使用或者都没有使用填充错误基本上说&#34;选择一个& #34;

经过这次检查。我需要query.php或query2.php来填充#inputhere div中包含php.php的位置,我需要它从该div中清除php.php,然后根据输入附加查询或查询2。

更新:

<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script>
$(document).ready(function () {
    var $article = null;
    $('#searchform').submit(function () {
        if ($article == null) {
            $("#inputhere").load("query.php").appendTo('.inputhere');
        }
    });
});
</script>

不改变我的表单中的任何内容,我现在尝试将表单加载到div中,但它仍然会立即将我重定向到显示query.php的新页面,我可以看到它将它加载到div中在重定向之前。有什么想法吗?

最后一个:

<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script>
$(document).ready(function () {
    var $article = null;
    $('#searchform').submit(function (e) {
        e.preventDefault();
        if ($article == null) {
            $("#inputhere")).html('Loading...').load('query.php');
        }
    });
});
</script>

它仍在单独的页面上加载查询。在我的服务器上可以看到@ www.masswarveterans.com

1 个答案:

答案 0 :(得分:2)

这样的事情就是你想要的:

$('#searchform').submit(e) {
    e.preventDefault();
    var business = $('#inpBusiness').val().trim();
    var location = $('#inpLocation').val().trim();

    if(
        (business == '' && location == '') // both empty
        ||
        (business != '' && location != '') // both not empty
    ) {
        alert('Pick one');
        return false;
    }

    var searchTerm = 'business';
    var action = 'query2.php';
    // The above definitions correspond to the implicit opposite of the statement
    // below. Overriding where necessary.
    if(location != '' && business == '') {
        action = 'query.php';
        searchTerm = location;
    }

    // Perform AJAX request to populate your div
    $('#inputhere').html('Loading...').load(action, {
        searchTerm: searchTerm,
    }, function(ret) {
        // If you need a callback on complete, use this.
    });
});

然后是您的PHP代码,使用$_POST['searchTerm']来帮助您决定要显示的内容。


期待......

一旦你弄清楚如何做到这一点,你应该重新开始并更好地设计它。如果您只有一个搜索脚本,并且有一个地方可以输入搜索词,那么对于您的应用程序来说,它会好得多。如评论中所述,使用无线电选择或下拉框来定义它应该是哪种搜索类型。

如果您这样做,稍后当您决定添加十种类型的搜索时,您将能够轻松地执行此操作,而无需为jQuery添加每种新类型的条件。

您最终会执行这样的AJAX请求(例如):

$('#searchform').submit(e) {
    e.preventDefault();
    if($('#search_term').val().trim() == '') {
        alert('Enter something man...');
        return false;
    }
    $('#inputhere').html('Loading...').load('search_file.php', {
        search_term: $('#search_term').val().trim(),
        search_type: $('#search_type :selected').val();
    });
});
相关问题