admin管理员组

文章数量:1391925

I am using jQuery for the first time. I've been searching a lot but have not gotten the answer.

I want to insert an input Type at id="star" and with value="score Star" but I'm not able to do that. Score returns the numeric value. I think the problem is in the value attribute, but I don't know where. Thanks In Advance.

Here's the code:

<script type="text/javascript">
            $(function() {
                $('#click').raty({
                    click: function(score) {
                        document.getElementById('star').innerHTML="<input type='text' name='type' value='"score+ "' Star >";
                        //alert( 'score: ' + score);

                        }
                    });
                });
</script>



          <label>Deal Type:</label>
         <div id="click"></div><span  id="star"></span></div>

I am using jQuery for the first time. I've been searching a lot but have not gotten the answer.

I want to insert an input Type at id="star" and with value="score Star" but I'm not able to do that. Score returns the numeric value. I think the problem is in the value attribute, but I don't know where. Thanks In Advance.

Here's the code:

<script type="text/javascript">
            $(function() {
                $('#click').raty({
                    click: function(score) {
                        document.getElementById('star').innerHTML="<input type='text' name='type' value='"score+ "' Star >";
                        //alert( 'score: ' + score);

                        }
                    });
                });
</script>



          <label>Deal Type:</label>
         <div id="click"></div><span  id="star"></span></div>
Share edited Dec 13, 2012 at 18:36 Corey Adler 16.2k18 gold badges69 silver badges80 bronze badges asked Dec 13, 2012 at 18:11 Vaibhav AgarwalVaibhav Agarwal 581 silver badge9 bronze badges 1
  • value='"score+ "' <--- you miss a '+' – Pablo Martinez Commented Dec 13, 2012 at 18:14
Add a ment  | 

4 Answers 4

Reset to default 2

You're not writing the code correctly

.innerHTML="<input type='text' name='type' value='"score+ "' Star >";

It should be

.innerHTML="<input type='text' name='type' value='"+score+" Star' >";

You're not concatenating the string, or quoting the value correctly

.innerHTML="<input type='text' name='type' value='"score+ "' Star >";

should be

.innerHTML="<input type='text' name='type' value='" + score + " Star' >";
<script type="text/javascript">
            $(function() {
                $('#click').raty({
                    click: function(score) {
                        document.getElementById('star').innerHTML="<input type='text' name='type' value='"+score+ " Star'>";
                        //alert( 'score: ' + score);

                        }
                    });
                });
</script>



          <label>Deal Type:</label>
         <div id="click"></div><span  id="star"></span></div>

A better approach is to actually create an input DOM object and append it:

$('#star').append($('<input/>').attr({
    type: "text",
    name: "type",
    value: score + " Star"
}));

本文标签: javascriptInsert Input Box Using innerHTML property of jQueryStack Overflow