A
A
Andrei ??????2015-07-07 01:09:44
JavaScript
Andrei ??????, 2015-07-07 01:09:44

How to find out what the form is filled with initially?

Hello.

Essence: there is a column which is divided into two parts, upper and lower. At the top there is a form that, when writing text into it, makes a dynamic search using ajax and the result displays blocks in the lower part. When nothing is written in the form, it displays everything that is in the database.
Problem: when you visit the site for the first time, nothing is shown at the bottom of the column, and if you write something in the form and then delete it, everything is shown as intended.
Question: What is wrong? What did I do or implement wrong?

the form looks like:

<form class="head11" action="PHP/search.php" method="post" name="form" onsubmit="return false;">
            <input name="search" type="text" id="search">
        </form>

javascript:
$(function() {
    $("#search").keyup(function(){
        var search = $("#search").val();
      var  len=search.toString().length;

        if (len==null || len==0) {
                   $.ajax({
                    type: "POST",
                    url: "PHP/first.php",
                    cache: false,                       
                    success: function(response){
                        $("#resSearch").html(response);}
                });
                }
                else{
                    $.ajax({
                    type: "POST",
                    url: "PHP/search.php",
                    data: {"search": search},
                    cache: false,                       
                    success: function(response){
                        $("#resSearch").html(response);}
                });
                }//else кончается
                return false; 
                     
    });
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sokolov, 2018-10-31
@sergiks

I don’t want to delve into the code, but you can optimize this:
Upd. I made such a decision, it passed. Not optimized, full enumeration is underway.

spoiler
function chooseBestSum(t, k, ls) {

  /**
   * make next combination of N on bits in a 32-bit integer
   */
  function nextPerm(x) {
    // via https://stackoverflow.com/questions/506807/creating-multiple-numbers-with-certain-number-of-bits-set
    if (x === 0) return 0;
    const smallest     = (x & -x);
    const ripple       = x + smallest;
    const new_smallest = (ripple & -ripple);
    const ones         = ((new_smallest/smallest) >> 1) - 1;
    return ripple | ones;
  }

  let bestSum = null, bestN;
  const len = ls.length;
  if (len > 31) throw "Too many (over 31) options for this algorithm";
  const maxmask = (1 << len) - 1;
  
  if (len < k) return null; // not enough distances
  
  ls.sort((a, b) => a - b); // todo: skip checking rest of combinations once solid selection of elements exceeds t

  let mask = (1 << k) - 1; // initial mask value with k less significant bits on
  
  let sum, pos, n;
  while(true) {
    for(sum = 0, n = 0, pos = 0; pos < 32; pos++) {
      if (mask & (1 << pos)) {
        sum += ls[pos];
        if (++n === k) break;
      }
    }

    if (sum > bestSum && sum <= t) {
      bestSum = sum;
      bestN = mask;
    }
    
    mask = nextPerm(mask);
    
    if (mask > maxmask) break;
    if (mask < 0) break;
  }
  
  return bestSum;
}

1
1alexandr, 2015-07-07
@Baccabu

It's better to write like thiselement.on('keyup', callback)
Take out your function in a variable and call it.

$(function() {
    var func = function(){
        var search = $("#search").val();
      var  len=search.toString().length;

        if (len==null || len==0) {
                   $.ajax({
                    type: "POST",
                    url: "PHP/first.php",
                    cache: false,                       
                    success: function(response){
                        $("#resSearch").html(response);}
                });
                }
                else{
                    $.ajax({
                    type: "POST",
                    url: "PHP/search.php",
                    data: {"search": search},
                    cache: false,                       
                    success: function(response){
                        $("#resSearch").html(response);}
                });
                }//else кончается
                return false; 
                     
    };

   func();

    $("#search").on('keyup', func);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question