连接输入值和String变量

时间:2021-03-21 20:20:03

I am trying to concatenate the value of the input with the String variable. For instance: (lets say that the input with an id 'input1' has the value of 'tttttt')

我试图将输入的值与String变量连接起来。例如:(假设id为'input1'的输入值为'tttttt')

var test = 'blabla' + $('#input1').val() + 'blabla';

So the expected result (at least for me) should be 'blablattttttblabla'. The problem is that it does not insert the value of that input in the middle so the result is 'blablablabla'. Does someone has an idea what I am doing wrong?

所以预期的结果(至少对我而言)应该是'blablattttttblabla'。问题是它没有在中间插入该输入的值,因此结果是'blablablabla'。有人知道我做错了什么吗?

2 个解决方案

#1


4  

Try

<input type="text" name="text" id="input1" value="ttttttt"/>

JS

<script>
$(document).ready(function(){
    var test = 'blabla' + $('#input1').val() + 'blabla';
    alert(test);
});
</script>

#2


2  

What it seems to me is that either you are missing a jQuery library at the top or this script is not called in the doc ready handler:

在我看来,你要么在顶部缺少一个jQuery库,要么在doc ready处理程序中没有调用这个脚本:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
   $(function () {
      var test = 'blabla' + $('#input1').val() + 'blabla';
      alert(test);
   });
</script>

Demo Fiddle

Be sure that your #input1 has a value contained in.

确保您的#input1包含一个值。

#1


4  

Try

<input type="text" name="text" id="input1" value="ttttttt"/>

JS

<script>
$(document).ready(function(){
    var test = 'blabla' + $('#input1').val() + 'blabla';
    alert(test);
});
</script>

#2


2  

What it seems to me is that either you are missing a jQuery library at the top or this script is not called in the doc ready handler:

在我看来,你要么在顶部缺少一个jQuery库,要么在doc ready处理程序中没有调用这个脚本:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
   $(function () {
      var test = 'blabla' + $('#input1').val() + 'blabla';
      alert(test);
   });
</script>

Demo Fiddle

Be sure that your #input1 has a value contained in.

确保您的#input1包含一个值。