简体   繁体   中英

How to put a string in input type=“text” with jQuery?

I don't understand why my code doesn't work: I try to put a string into input type="text" with jQuery and it doesn't work. It separates different elements of the string when there are space in properties for the input.

My jQuery Code:

<script type="text/javascript">
    $(document).ready(function() {
        $(".btnEditSerie").click(function (e) {            
            e.preventDefault();
            var tr = $(this).closest('tr');
            var $GroupingId = tr.find('#item_GroupingId').val()
            localStorage['GroupingId'] = $GroupingId;
            var $Title = tr.find('#item_Title').val();
            var $Description = tr.find('#item_Description').val();
            var $Image = tr.find('#item_Image').val();
            $("#b").hide("slow");
            $("#btnretour").show();
            $('#SerieEdit').append("<label id=" + 'test2' + ">" + "Modification de :" + " " + $Title+ "</label>");
            $('#TextEdit').html("<label>" + "Titre :" + " " + "</label>" + "<input type=" + 'text' + " value=" + $Title + " id=" + 'SerieNewName' + " />"
                                 + "<label>" + "Description :" + " " + "</label>" + "<input type=" + 'text' + " value=" + $Description.toString() + " id=" + 'SerieNewDescription' + "/>"
                                 + "<label>" + "Image :" + " " + "</label>" + "<input type="+'file'+" value="+$Image.toString()+ " id=" +'SerieNewImage'+"/>");
            $("#SerieEdit").show();
            $("#TextEdit").show();   
        })

        $('#btnRet').click(function() {
            $("#b").show("slow");
            $("#SerieEdit").hide();
            $("#TextEdit").hide();
            $("#SerieEdit").empty();
            $("#TextEdit").empty();
            $("#btnretour").hide();
        })
    });   
</script>

With val() , for instance $Title must be a string, and when i put $Title in the .Append() it returns a thing like that:

My sad html code:

<h2>EditRefSerie</h2>
<div id="SerieEdit">
    <div id="TextEdit">
        <label>Titre : </label>
        <input id="SerieNewName" type="text" thrones="" of="" value="Games">
        <label>Description : </label>
        <input id="SerieNewDescription/" type="text" thrones="" of="" value="games">
        <label>Image : </label>
        <input id="SerieNewImage/" type="file" value="Dessin1.jpg">
    </div>
<div id="btnretour" style="">

For the string "Games of thrones" it returns:

<input id="SerieNewName" type="text" thrones="" of="" value="Games">

Have you ideas how to fix it? Do I use jQuery correctly?

You're forgetting quotes:

Your JS :

$('#TextEdit').html("<label>" + "Titre :" + " " + "</label>" + "<input type=" + 'text' + " value=" + $Title + " id=" + 'SerieNewName' + " />" 
+ "<label>" + "Description :" + " " + "</label>" + "<input type=" + 'text' + " value=" + $Description.toString() + " id=" + 'SerieNewDescription' + "/>" 
+ "<label>" + "Image :" + " " + "</label>" + "<input type="+'file'+" value="+$Image.toString()+ " id=" +'SerieNewImage'+"/>");

Should be :

$('#TextEdit').html("<label>" + "Titre :" + " " + "</label>" + "<input type=" + 'text' + " value='" + $Title + "' id=" + 'SerieNewName' + " />" 
+ "<label>" + "Description :" + " " + "</label>" + "<input type=" + 'text' + " value='" + $Description.toString() + "' id=" + 'SerieNewDescription' + "/>" 
+ "<label>" + "Image :" + " " + "</label>" + "<input type="+'file'+" value='"+$Image.toString()+ "' id=" +'SerieNewImage'+"/>");

Note the added quotes for the value attributes. Bonus tip: there's no need to put input types in a concatenated string. It's much more readable if you do:

"<input type='text' value='" + val + "' />"

instead of:

"<input type='" + 'text' + "' value='" + val + "' />" 

like you're doing.

您没有在属性周围正确使用引号,例如

$('#SerieEdit').append("<label id='" + test2 + "'>" + "Modification de :" + " " + $Title+ "</label>");

ID is unique in HTML page. So you don't need to "find" for retrieve your value.

Also, the variable don't need a "$"

Do something like:

$(document).ready(function () {
    $(".btnEditSerie").click(function (e) {            
        e.preventDefault();
        var GroupingId = $('#item_GroupingId').val(); // not need to find if you have a id.. id is unique in the HTML page.
        localStorage['GroupingId'] = $GroupingId;
        var Title = $('#item_Title').val();
        var Description = $('#item_Description').val();
        var Image = $('#item_Image').val();
        $("#b").hide("slow");
        $("#btnretour").show();
        $('#SerieEdit').append("<label id='test2'>Modification de : " + Title + "</label>");
        $('#TextEdit').html("<label>Titre : </label> <input type='text' value=" + Title + " id='SerieNewName' /> <label> Description : </label><input type='text' value=" + Description + " id='SerieNewDescription'/><label>Image : </label><input type='file' value="+ Image + " id='SerieNewImage'/>");
        $("#SerieEdit").show();
        $("#TextEdit").show();
    });


    $('#btnRet').click(function(){
        $("#b").show("slow");
        $("#SerieEdit").hide();
        $("#TextEdit").hide();
        $("#SerieEdit").empty();
        $("#TextEdit").empty();
        $("#btnretour").hide();
    })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM