简体   繁体   English

如何显示选择框 <p> 我在javascript中做p.text(“ test”)吗?

[英]How do i show a selectbox when it is in <p> and i do p.text(“test”) in javascript?

In am working with MVC and i want to show a selectbox 在与MVC合作时,我想显示一个选择框

   <p id="selectedTrigger">
       <select name="devices" id="devices"><!-- Here i get the options with javascript --></select>
   </p>

If i do this it works, but when i do in javascript the next line of code: 如果执行此操作,则可以,但是当我在javascript中执行下一行代码时:

    $('#selectedTrigger').text("TEST");

It doesn't show the selectbox anymore, only the text TEST 它不再显示选择框,仅显示文本TEST

How can i solve this problem? 我怎么解决这个问题?

What happens is you replace everything between <p id="selectedTrigger"> and </p> . 发生的情况是替换了<p id="selectedTrigger"></p> If you are using jQuery then use .html() method to replace content inside it or use .append() to add content inside your <p> . 如果您使用的是jQuery,则使用.html()方法替换其中的内容,或使用.append()<p>内部添加内容。

So in your instance you would have to say: 因此,在您的情况下,您将不得不说:

$('#selectedTrigger').append("TEST");

Note that this will result in following code: 请注意,这将导致以下代码:

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

There is also .prepend() method that allows you to append content before any content you already have. 还有一个.prepend()方法,可让您在已有内容之前追加内容。

$('#selectedTrigger').prepend("TEST");

This would give you: 这将为您提供:

<p ...>
  TEXT
  <select ..></select>
</p>

Solution: But since you are trying to add options to your <select> inside your <p> you should really use this: 解决方案:但是,由于您要尝试在<p>内的<p> <select>添加选项,因此您应该使用此选项:

$('#devices').append("TEST");

This will give you: 这将为您提供:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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