Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当选择框处于<;状态时,如何显示它;p>;我用javascript做p.text(“test”)吗?_Javascript_Jquery_Drop Down Menu - Fatal编程技术网

当选择框处于<;状态时,如何显示它;p>;我用javascript做p.text(“test”)吗?

当选择框处于<;状态时,如何显示它;p>;我用javascript做p.text(“test”)吗?,javascript,jquery,drop-down-menu,Javascript,Jquery,Drop Down Menu,在我使用MVC时,我想显示一个选择框 <p id="selectedTrigger"> <select name="devices" id="devices"><!-- Here i get the options with javascript --></select> </p> 它不再显示选择框,只显示文本测试 如何解决此问题?发生的事情是替换和之间的所有内容。如果您使用的是jQuery,则使用.html

在我使用MVC时,我想显示一个选择框

   <p id="selectedTrigger">
       <select name="devices" id="devices"><!-- Here i get the options with javascript --></select>
   </p>
它不再显示选择框,只显示文本测试


如何解决此问题?

发生的事情是替换

之间的所有内容。如果您使用的是jQuery,则使用
.html()
方法替换其中的内容,或者使用
.append()
中添加内容

因此,在你的例子中,你必须说:

$('#selectedTrigger').append("TEST");
请注意,这将导致以下代码:

<p ...>
  <select ..></select>
  TEST
</p>
这将给你:

<p ...>
  TEXT
  <select ..></select>
</p>
这将为您提供:

<p ...>
  <select ..>
    TEST
  </select>
</p>

试验


这是因为当你调用.text(“TEST”);它取代了

的内容,您想做什么?在选择框后显示测试?也许您想在p后面添加文本“测试”?试试$(“#selectedTrigger”)。追加(“测试”)instead@TomHorwood好了:)谢谢@kellax with.html它工作得很好,我可以删除很多行

<p ...>
  TEXT
  <select ..></select>
</p>
$('#devices').append("TEST");
<p ...>
  <select ..>
    TEST
  </select>
</p>