Var and input variable using jQuery

I have added the following code to my program.

var text = $( this ).text();
$( "input" ).val( text );

Although instead of filling a gap inputin my form, he changes my button submit. How can I get this to fill in my text input type=textinstead of rewriting mine input type=submit?

My program code is as follows.

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>

    <cfquery datasource="AccessTest" name="qTest">
        SELECT P.Account, P.Image, P.Image_ID, C.Remarks, C.Users, C.Accounts, C.Date_Time
        FROM PictureDB AS P
        INNER JOIN CommentsDB AS C
        ON C.Image_ID = P.Image_ID
        ORDER BY P.Image_ID
    </cfquery>

    <script src="http://code.jquery.com/jquery-2.0.3.js"></script>
    <script>
        $(document).ready(function(){
            var images = {
                <cfoutput query="qTest" group="Image_ID">
                    "#qTest.Image_ID#": {
                        "image": "#qTest.Image#",
                        "remarks": [
                            <cfoutput>
                                "#qTest.Users#, #qTest.Date_Time# <br> #qTest.Remarks# <br> </br>",
                            </cfoutput>
                        ]
                    },
                </cfoutput>
            };
            $("button").click(function(event) {
                event.preventDefault();
                var id = $(this).data("id");
                var src = images[id].image;
                var desc = images[id].remarks.toString();
                var text = $( this ).text();
                $( "input" ).val( text );
                $("#theImage").attr("src", src).removeClass("hide");
                $("#theDescription").html(desc).removeClass("hide");
            });
        });
    </script>
  </head>
  <body>
    <cfoutput query="qTest" group="Account">
      <button data-id="#qTest.Image_ID#">#qTest.Account#</button>
    </cfoutput>
    <cfform name="InsertComments" id="InsertComments">
      <fieldset>
        <div id="container">
          <div id="mainContent">
            <textarea name="Remarks" cols="55" rows="4" label="Tour Description"
                enabled="no" required="yes" validateat="OnSubmit" 
                message="Please enter your comment here">
            </textarea>
            <input type="text" name="Image_ID" message="Please enter Account Here."
                validateat="onSubmit" required="yes" id="Image_ID" size="10"
                maxlength="60">
            </input>
            <input type="submit" name="insertComments" value="Insert Comments"
                id="submit">
            </input>
          </div>
        </div>  
      </fieldset>
    </cfform>
    <cfif IsDefined("form.InsertComments")>
      <cfquery datasource="AccessTest">
        INSERT INTO CommentsDB (Remarks, Image_ID, Date_Time )
        VALUES (
            <cfqueryparam value="#form.Remarks#" cfsqltype="CF_SQL_LONGVARCHAR">
          , <cfqueryparam value="#form.Image_ID#" cfsqltype="cf_sql_integer">
          , <cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp">
        )
      </cfquery>
    </cfif>

    <img id="theImage" class="hide" />
      <div id="theDescription" class="hide"></div>
  </body>
</html> 
-1
source share
3 answers

Try this instead.

$('input[type="text"]').val(text);

or since you have an identifier, you can use the identifier of your input text box.

$("#Image_ID").val(text);
+2
source

I think you can use $( "#Image_ID" ).val( text );instead$( "input" ).val( text );

+3
source

jQuery( "[attribute='value']" )

: .

: . , .

$("input[type='text']").val(text);

http://api.jquery.com/attribute-equals-selector/

Since you have id, you can use it to select a text field, this is the best way

$("#Image_ID").val(text);
+1
source

Source: https://habr.com/ru/post/1523639/


All Articles