-1

hope all is going well, please help me with a question. I have the following form html code:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="jsexterno.js"></script>
<meta charset="utf-8">
<title>Motor de busqueda instantaneo</title>
</head>
<body>
<form id="foo1" accept-charset="utf-8" method="POST" action="gims_cd_fcom.php">
<input type="text" name="pdcode_foo1" id="pdcode_foo1" placeholder="" maxlength="30" OnKeyUp="esearch(this);"/>
<input type="text" name="pdname_foo1" id="pdname_foo1" placeholder="" maxlength="30"/>
<input type="text" name="pduom_foo1" id="pduom_foo1" placeholder="" maxlength="30"/>
<input type="text" name="pdcom_foo1" id="pdcom_foo1" placeholder="" maxlength="30"/>
<input type="hidden" name="handlerfn_foo1" id="handlerfn_foo1" value="wrt"/>
<input type="submit" id="foo1_btn" value="Agregar">
</form>
<div id="resultadoBusqueda"></div>
<div id="ShowDBReg"></div>
</body>
</html>

And I have the following JS code:

function esearch() {
    var FieldtxtID = $(this).attr('id');
    var FieldTxtVal = $(document).GetElementById('FieldtxtID').val();
    if (FieldTxtVal != "") {
        $.post("gims_cd_fcom.php", {handlerfn_foo1: 'sengine', handl_keyword: FieldTxtVal, handl_data: FieldtxtID}, function(mensaje) {
            $("#ShowDBReg").html(mensaje);
         }); 
    } else { 
        ShwDB();
    };
};

But I cannot reach the ID of txt, whats is wrong?

  • first of all are you sure that $(this) is the tag you want to explore? use alert or console.log() to check if it is correct as you desire or not – Gahan Feb 08 '18 at 06:17
  • https://stackoverflow.com/questions/3239598/how-can-i-get-the-id-of-an-element-using-jquery – V-Wanderer Feb 08 '18 at 06:17
  • This is wrong: `var FieldTxtVal = $(document).GetElementById('FieldtxtID').val();` change to this: `$('#FieldtxtID').val();`. I strongly suggest that you read tutorials on the fundamentals of JS/jQ. Hey where is `'FieldtxtID'? You must stop cut and pasting stuff and Frankstiening code. – zer00ne Feb 08 '18 at 06:51

1 Answers1

0

From what I have seen, it looks like it should work.

Even so, I recommend you check a few things:

  1. Make sure that your code is surrounded by a

    $(function() {

    });

to make sure that jQuery is loaded before you use it.

  1. Make sure that your jQuery script tag is always loaded above your other JS, and double check to make sure your jQuery script tag does not contain an async attribute.

  2. I further recommend that you test your code to make sure that the if statement and post request surrounding the jQuery actually executes. You can test this by placing a console.log('debug1') right before your jQuery code. Evidently, if you see "debug1" in your console, you will know that the html of your div should be changing.

arjunpat
  • 659
  • 5
  • 10